Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

python - How do I get the scraped data in correct order with threading?

I am learning scraping with python. The problem with the given below code is that it does not show or save the data in correct order. As I am doing it with the help of threading.

Whenever I build a scraper, I face the same problem. Please tell me the way to fix it.

import requests
from bs4 import BeautifulSoup as bs
import threading



def scrape(page):
    baseUrl = "http://quotes.toscrape.com/page/"
    html = requests.get(baseUrl+str(page))
    soup = bs(html.text, "lxml")
    author = soup.find_all("small", class_="author")
    for auth in author:
        print(auth.text)
    print("The page number is "+ str(page)) #printing the page number 

ml = [x for x in range(2, 12)]

threadList = []

for u in ml:
    t = threading.Thread(target=scrape, args=(u,))
    t.start()
    threadList.append(t)

for b in threadList:
    b.join()
question from:https://stackoverflow.com/questions/65945047/how-do-i-get-the-scraped-data-in-correct-order-with-threading

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...