Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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_Beautifulsoup_Urllib_Large Data - Fatal编程技术网

Python';索引器:列表索引超出范围';在分析大型数组时,请使用

Python';索引器:列表索引超出范围';在分析大型数组时,请使用,python,beautifulsoup,urllib,large-data,Python,Beautifulsoup,Urllib,Large Data,我不断发现以下错误: Traceback (most recent call last): File "C:\Users\User\Documents\Project.py", line 100, in <module> parseData(array) File "C:\Users\User\Documents\Project.py", line 91, in parseData name2 = pageSoup.findAll('div', {'class

我不断发现以下错误:

Traceback (most recent call last):
  File "C:\Users\User\Documents\Project.py", line 100, in <module>
    parseData(array)
  File "C:\Users\User\Documents\Project.py", line 91, in parseData
    name2 = pageSoup.findAll('div', {'class': 'item-title'})[0].string
IndexError: list index out of range

感谢您抽出时间查看此内容,非常感谢您的帮助!:)

有一个索引器,我认为这两个问题都可以通过以下方式解决:


导入urllib.request
def解析数据(url):
以urllib.request.urlopen('url')作为响应:
如果回答:
responseContent=response.read()
pageSoup=BeautifulSoup(来自_encoding=“utf-8”的响应内容“html.parser”)
如果https://example.com'在url中:
尝试:
name1=pagesoop.findAll('h3',{'class':'tb main title'})[0]。字符串
price1=pageSoup.findAll('em',{'class':'tb rmb num'})[0]。字符串
除索引器e外:
通过
其他:
link=url
打印('从'+str(link)+'检索数据…\n'+str(name1)+':'+str(price1))
收益率(链接、名称1、价格1)
艾利夫https://example2.com'在url中:
尝试:
name2=pageSoup.findAll('div',{'class':'item title'})[0]。字符串
price2=pageSoup.findAll('span',{'class':'curprice'})[0]。字符串
除索引器e外:
通过
其他:
打印('从'+str(link)+'检索数据…\n'+str(name2)+':'+str(price2))
收益率(链接、名称2、价格2)
URL=[“此处URL列表”]
如果_uname _uu==“main”:
对于url中的url_uu:
链接、名称、价格=解析数据(url)
将open('output.txt','a',encoding='utf-8')作为f:
f、 写入('\n'+str(链接)+'\n'+str(名称)+'\n'+str(价格)+'\n')

这段代码添加了一个管理器上下文和一些检查,可以避免一些错误,从而改进上述响应

导入urllib.request
从bs4导入BeautifulSoup
从集合导入namedtuple
Data=namedtuple('Data','linkname-price')
def解析数据(url):
链接=无
name=None
价格=无
使用urllib.request.urlopen(url)作为响应:
如果回答:
#responseContent=response.read()
pageSoup=BeautifulSoup(来自_encoding=“utf-8”的响应“html.parser”)
如果https://example.com'在url中:
尝试:
name=pagesoop.findAll('h3',{'class':'tb main title'})[0]。字符串
price=pageSoup.findAll('em',{'class':'tb rmb num'})[0]。字符串
除索引器e外:
通过
艾利夫https://example2.com'在url中:
断点()
尝试:
name=pagesoop.findAll('div',{'class':'item title'})[0]。字符串
price=pageSoup.findAll('span',{'class':'curprice'})[0]。字符串
除索引器e外:
通过
link=url
打印('从'+str(链接)+'.\n'+str(名称)+':'+str(价格)检索数据))
返回数据(链接=链接,名称=名称,价格=价格)
URL=[”https://www.yahoo.com", "https://www.google.com"]
如果名称=“\uuuuu main\uuuuuuuu”:
对于url中的url_uu:
data=parseData(url_382;)
如果data.link和data.name与data.price:
将open('output.txt','a',encoding='utf-8')作为f:
f、 写入('\n'+str(链接)+'\n'+str(名称)+'\n'+str(价格)+'\n')

您应该仔细阅读异常处理。看起来你要刮的某个页面挡住了你,或者没有你要找的元素。哦,我看到问题了,谢谢你澄清。我很困惑!在文件中它是从哪里来的?我找不到了。非常感谢你的理解和高效!我用这种方法做了另一个修改,做了类似的事情,结果证明速度更快。再次感谢你。非常感谢!
def parseData(urls):
    f = io.open('output.txt', 'a', encoding='utf-8')
    for url in urls:
        response = urllib.request.urlopen(url)
        responseContent = response.read()
        pageSoup = BeautifulSoup(responseContent, 'html.parser', from_encoding="utf-8")
        if 'https://example.com' in url:
            name = pageSoup.findAll('h3', {'class': 'tb-main-title'})[0].string
            price = pageSoup.findAll('em', {'class': 'tb-rmb-num'})[0].string
            link = url
            print('Retriving data from ' + str(link) + '...\n' + str(name) + ':' + str(price))
            f.write('\n' + str(link) + '\n' + str(name) + '\n' + str(price) + '\n')

        elif 'https://example2.com' in url:
            name2 = pageSoup.findAll('div', {'class': 'item-title'})[0].string
            price2 = pageSoup.findAll('span', {'class': 'cur-price'})[0].string
            print('Retriving data from ' + str(link) + '...\n' + str(name2) + ':' + str(price2))
            f.write('\n' + str(link) + '\n' + str(name2) + '\n' + str(price2) + '\n')