python程序没有运行

问题描述 投票:0回答:3

我通过PyCharm CE 2018.3.4在Python中编写了一些脚本,但是当我运行这个脚本时,它永远不会向我显示结果,它永远不会结束。是因为PyCharm还是因为脚本。

import requests
from bs4 import BeautifulSoup


def trade_spider(max_pages):
    page = 1
    while page <=1:
        url = "https://www.ebay.com/sch/i.html?_from=R40&_nkw=car&_sacat=0&_pgn="+str(page)
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text,"html.parser")
        for link in soup.findAll('a',{'class' :'item-name' }):
            href = link.get('href')
            title = link.string
            print(href)
            print(title)

trade_spider(2)
python
3个回答
0
投票

首先,你有一个无限循环:

page = 1
while page <= 1:
    # Code in which page never changes

page总是1,所以你无法离开循环。

至于不打印任何东西,你不断从该网站获取第一页。一个简单的print结果显示该页面上没有class条目。因此,没有什么可印刷的。

试试这个:

for page in range(1, max_pages+1):
    url = "https://www.ebay.com/sch/i.html?_from=R40&_nkw=car&_sacat=0&_pgn="+str(page)

0
投票

你的while循环的主要陈述是:while page <=1:但它永远不会增加,'page'值始终为1.如果你想要抓取2个页面,就像你想要做的那样我相信它应该是这样的:

def trade_spider(max_pages):
    page = 1
    #Loop until page number equals max_pages value
    while page <= max_pages:
         url = "https://www.ebay.com/sch/i.html?_from=R40&_nkw=car&_sacat=0&_pgn="+str(page)
         source_code = requests.get(url)
         plain_text = source_code.text
         soup = BeautifulSoup(plain_text,"html.parser")
         for link in soup.findAll('a',{'class' :'item-name' }):
             href = link.get('href')
             title = link.string
             print(href)
             print(title)
         #Increment page so it crawls next one on each iteration
         page+=1

0
投票

这是一个代码问题。您正在设置page = 1并且从不递增该值。所以while循环永远不会结束。

© www.soinside.com 2019 - 2024. All rights reserved.