Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用两个线程在脚本中执行两个函数_Python_Python 3.x_Web Scraping_Python Multithreading - Fatal编程技术网

Python 无法使用两个线程在脚本中执行两个函数

Python 无法使用两个线程在脚本中执行两个函数,python,python-3.x,web-scraping,python-multithreading,Python,Python 3.x,Web Scraping,Python Multithreading,我已经使用python结合线程创建了一个scraper,以加快执行速度。scraper应该解析网页中以不同字母结尾的所有可用链接。它确实解析了它们 但是,我希望再次使用线程解析这些链接中的所有名称和电话号码。第一部分我可以使用Thread运行,但我不知道如何创建另一个Thread来执行脚本的后一部分 我本可以将它们封装在一个线程中,但我的目的是了解如何使用两个线程来执行两个函数 第一部分:我尝试了下面的方法,效果很好 我的问题是:如何在另一个线程中使用子链接() import requests

我已经使用python结合
线程
创建了一个scraper,以加快执行速度。scraper应该解析网页中以不同字母结尾的所有可用链接。它确实解析了它们

但是,我希望再次使用
线程
解析这些链接中的所有
名称
电话
号码。第一部分我可以使用
Thread
运行,但我不知道如何创建另一个
Thread
来执行脚本的后一部分

我本可以将它们封装在一个
线程中
,但我的目的是了解如何使用两个
线程来执行两个函数

第一部分:我尝试了下面的方法,效果很好

我的问题是:如何在另一个
线程中使用
子链接()

import requests
import threading
from lxml import html

main_url = "https://www.houzz.com/proListings/letter/{}"

def alphabetical_links(mainurl):
    response = requests.get(link).text
    tree = html.fromstring(response)
    return [container.attrib['href'] for container in tree.cssselect(".proSitemapLink a")]

if __name__ == '__main__':
    linklist = []
    for link in [main_url.format(chr(page)) for page in range(97,123)]:
        thread = threading.Thread(target=alphabetical_links, args=(link,))
        thread.start()
        linklist+=[thread]

    for thread in linklist:
        thread.join()
import requests
import threading
from lxml import html

main_url = "https://www.houzz.com/proListings/letter/{}"

def alphabetical_links(mainurl):
    response = requests.get(link).text
    tree = html.fromstring(response)
    return [container.attrib['href'] for container in tree.cssselect(".proSitemapLink a")]

def sub_links(process_links):
    response = requests.get(process_links).text
    root = html.fromstring(response)

    for container in root.cssselect(".proListing"):
        try:
            name = container.cssselect("h2 a")[0].text
        except Exception: name = ""
        try:
            phone = container.cssselect(".proListingPhone")[0].text
        except Exception: phone = ""
        print(name, phone)

if __name__ == '__main__':
    linklist = []
    for link in [main_url.format(chr(page)) for page in range(97,123)]:
        thread = threading.Thread(target=alphabetical_links, args=(link,))
        thread.start()
        linklist+=[thread]

    for thread in linklist:
        thread.join()

您可以像启动第一个线程一样启动更多线程

from threading import Thread

t1 = Thread(target=alphabetical_links, kwargs={
    'mainurl':     link,
})
t1.start()

t2 = Thread(target=sub_links, kwargs={
    'process_links':     link,
})
t2.start()

您可以像启动第一个线程一样启动更多线程

from threading import Thread

t1 = Thread(target=alphabetical_links, kwargs={
    'mainurl':     link,
})
t1.start()

t2 = Thread(target=sub_links, kwargs={
    'process_links':     link,
})
t2.start()

尝试用自己的线程更新字母链接

import requests
import threading
from lxml import html

main_url = "https://www.houzz.com/proListings/letter/{}"


def alphabetical_links(mainurl):
    response = requests.get(mainurl).text
    tree = html.fromstring(response)
    links_on_page = [container.attrib['href'] for container in tree.cssselect(".proSitemapLink a")]
    threads = []
    for link in links_on_page:
        thread = threading.Thread(target=sub_links, args=(link,))
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()


def sub_links(process_links):
    response = requests.get(process_links).text
    root = html.fromstring(response)

    for container in root.cssselect(".proListing"):
        try:
            name = container.cssselect("h2 a")[0].text
        except Exception: name = ""
        try:
            phone = container.cssselect(".proListingPhone")[0].text
        except Exception: phone = ""
        print(name, phone)

if __name__ == '__main__':
    linklist = []
    for link in [main_url.format(chr(page)) for page in range(97,123)]:
        thread = threading.Thread(target=alphabetical_links, args=(link,))
        thread.start()
        linklist+=[thread]


    for thread in linklist:
        thread.join()

请注意,这只是如何管理“内部线程”的一个示例。由于许多线程同时启动,您的系统可能会由于资源不足而无法启动其中一些线程,您将得到
RuntimeError:cannotstart new thread
异常。在这种情况下,您应该尝试实现尝试用自己的线程更新
字母链接

import requests
import threading
from lxml import html

main_url = "https://www.houzz.com/proListings/letter/{}"


def alphabetical_links(mainurl):
    response = requests.get(mainurl).text
    tree = html.fromstring(response)
    links_on_page = [container.attrib['href'] for container in tree.cssselect(".proSitemapLink a")]
    threads = []
    for link in links_on_page:
        thread = threading.Thread(target=sub_links, args=(link,))
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()


def sub_links(process_links):
    response = requests.get(process_links).text
    root = html.fromstring(response)

    for container in root.cssselect(".proListing"):
        try:
            name = container.cssselect("h2 a")[0].text
        except Exception: name = ""
        try:
            phone = container.cssselect(".proListingPhone")[0].text
        except Exception: phone = ""
        print(name, phone)

if __name__ == '__main__':
    linklist = []
    for link in [main_url.format(chr(page)) for page in range(97,123)]:
        thread = threading.Thread(target=alphabetical_links, args=(link,))
        thread.start()
        linklist+=[thread]


    for thread in linklist:
        thread.join()

请注意,这只是如何管理“内部线程”的一个示例。由于许多线程同时启动,您的系统可能会由于资源不足而无法启动其中一些线程,您将得到
RuntimeError:cannotstart new thread
异常。在这种情况下,您应该尝试实现

是的,我知道这是多线程可以执行的方式,但我不能在上面的脚本中使用逻辑。你能告诉我,如果你认为上面的脚本是一个例子?谢谢。是的,我知道这是多线程可以执行的方式,但是我不能在我上面的脚本中使用逻辑。你能告诉我,如果你认为上面的脚本是一个例子?谢谢,谢谢你的剧本,先生。我需要知道逻辑,而你给了我很好的证明。顺便问一下,我应该在脚本中使用一点延迟吗?我认为延迟在这种情况下不会有多大的效率。。。ThreadingPool将允许您限制并发线程数-更适合于本例IMHOThanks for the script sir。我需要知道逻辑,而你给了我很好的证明。顺便问一下,我应该在脚本中使用一点延迟吗?我认为延迟在这种情况下不会有多大的效率。。。ThreadingPool将允许您限制并发线程数-更适合这种情况