如何使用线程以正确的顺序获取刮取的数据?

2024-04-16 16:57:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习用python进行刮削。下面给出的代码的问题是它没有按正确的顺序显示或保存数据。因为我是在穿线的帮助下做的

每当我制造刮刀时,我都会面临同样的问题。请告诉我怎么修

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()