Bs4: Trying to loop in diferent arrays with diferent lenghts. Get IndexError: list index out of range
Bs4: Trying to loop in diferent arrays with diferent lenghts. Get IndexError: list index out of range
使用 Beautifulsoup4 和 python3.7 我正在尝试使用链接循环一些数组。之后,想要从标签中获取一些文本。但是我在终端上传递代码时遇到错误。
这里是代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
import csv
my_url = "http://www.example.com"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
links = page_soup.select('dt > a[href]')
link = [tag.get('href') for tag in links]
i = 0
for i in range(0, 5000):
url = link[i]
Client = uReq(url)
pageHtml = Client.read()
Client.close()
pSoup = soup(pageHtml, "html.parser")
linkeas = pSoup.findAll(href=re.compile(my_url))
def linkas(href):
return href and re.compile("html").search(href) and re.compile(my_url).search(href)
linka = pSoup.findAll(href=linkas)
if linka != []:
linkia = [tag.get('href') for tag in linka]
linko = len(linkia)
j = 0
for j in range (0, linko):
curl = linkia[j]
cClient = uReq(curl)
pageHtml = cClient.read()
cClient.close()
Soup = soup(page_html, "html.parser")
country = Soup.select('.class > a:nth-of-type(3)')
countri = country[0].text.strip()
print(countri)
我已经尝试了好几天,但至今没有结果:
Traceback (most recent call last):
File "<stdin>", line 22, in <module>
IndexError: list index out of range
有人可以给点小费吗?
注意:
数组显示如下:
print(linkia)
['http://www.example/example/1.html']
['http://www.example/example/2.html']
['http://www.example/example/3.html', 'http://www.example/example/4.html',
'http://www.example/example/5.html', 'http://www.example/example/6.html',
'http://www.example/example/7.html', 'http://www.example/example/8.html',
'http://www.example/example/9.html', 'http://www.example/example/10.html',
'http://www.example/example/11.html', 'http://www.example/example/12.html',
'http://www.example/example/13.html', 'http://www.example/example/14.html',
'http://www.example/example/15.html', 'http://www.example/example/16.html',
'http://www.example/example/17.html', 'http://www.example/example/18.html',
'http://www.example/example/19.html']
['http://www.example/example/20.html', 'http://www.example/example/example/21.html',
'http://www.example/example/example/22.html']
['http://www.example/example/23.html']
非常感谢您的宝贵时间。非常感谢。将始终连接并快速响应。
变化:
i = 0
for i in range(0, 5000):
url = link[i]
只是:
for url in link:
然后可以去掉url = link[i]
当您没有 5000 个项目时,您实际上是在告诉它循环遍历列表中的 5000 个项目,因此 list index out of range
。你真的只是希望它循环遍历每个元素,直到它用完所有项目。你可以简单地说 for url in link:
然后你的另一个嵌套 for 循环也是如此。
变化:
j = 0
for j in range (0, linko):
curl = linkia[j]
至:
for curl in linkia:
我还要注意,如果您按照自己的方式设置它,则不需要将初始 i 或 j 设置为 = 0。因为您将 range/list 设置为从 0、5000 开始...for 循环会自动从 0 的第一个元素开始。但同样,那一点是无关紧要的,因为我不建议像那样遍历你的列表。它 a) 不健壮(每次进入该循环时您的列表中都需要恰好 5000 个项目),并且 b) 虽然它对您的第二个循环工作正常,因为您将范围设置为从 0 到列表,它确实是不必要的,因为您可以将其压缩为 1 行。
尝试:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
import csv
my_url = "http://www.example.com"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
links = page_soup.select('dt > a[href]')
link = [tag.get('href') for tag in links]
for url in link:
Client = uReq(url)
pageHtml = Client.read()
Client.close()
pSoup = soup(pageHtml, "html.parser")
linkeas = pSoup.findAll(href=re.compile(my_url))
def linkas(href):
return href and re.compile("html").search(href) and re.compile(my_url).search(href)
linka = pSoup.findAll(href=linkas)
if linka != []:
linkia = [tag.get('href') for tag in linka]
for curl in linkia:
cClient = uReq(curl)
pageHtml = cClient.read()
cClient.close()
Soup = soup(page_html, "html.parser")
country = Soup.select('.class > a:nth-of-type(3)')
countri = country[0].text.strip()
print(countri)
使用 Beautifulsoup4 和 python3.7 我正在尝试使用链接循环一些数组。之后,想要从标签中获取一些文本。但是我在终端上传递代码时遇到错误。
这里是代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
import csv
my_url = "http://www.example.com"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
links = page_soup.select('dt > a[href]')
link = [tag.get('href') for tag in links]
i = 0
for i in range(0, 5000):
url = link[i]
Client = uReq(url)
pageHtml = Client.read()
Client.close()
pSoup = soup(pageHtml, "html.parser")
linkeas = pSoup.findAll(href=re.compile(my_url))
def linkas(href):
return href and re.compile("html").search(href) and re.compile(my_url).search(href)
linka = pSoup.findAll(href=linkas)
if linka != []:
linkia = [tag.get('href') for tag in linka]
linko = len(linkia)
j = 0
for j in range (0, linko):
curl = linkia[j]
cClient = uReq(curl)
pageHtml = cClient.read()
cClient.close()
Soup = soup(page_html, "html.parser")
country = Soup.select('.class > a:nth-of-type(3)')
countri = country[0].text.strip()
print(countri)
我已经尝试了好几天,但至今没有结果:
Traceback (most recent call last):
File "<stdin>", line 22, in <module>
IndexError: list index out of range
有人可以给点小费吗?
注意:
数组显示如下:
print(linkia)
['http://www.example/example/1.html']
['http://www.example/example/2.html']
['http://www.example/example/3.html', 'http://www.example/example/4.html',
'http://www.example/example/5.html', 'http://www.example/example/6.html',
'http://www.example/example/7.html', 'http://www.example/example/8.html',
'http://www.example/example/9.html', 'http://www.example/example/10.html',
'http://www.example/example/11.html', 'http://www.example/example/12.html',
'http://www.example/example/13.html', 'http://www.example/example/14.html',
'http://www.example/example/15.html', 'http://www.example/example/16.html',
'http://www.example/example/17.html', 'http://www.example/example/18.html',
'http://www.example/example/19.html']
['http://www.example/example/20.html', 'http://www.example/example/example/21.html',
'http://www.example/example/example/22.html']
['http://www.example/example/23.html']
非常感谢您的宝贵时间。非常感谢。将始终连接并快速响应。
变化:
i = 0
for i in range(0, 5000):
url = link[i]
只是:
for url in link:
然后可以去掉url = link[i]
当您没有 5000 个项目时,您实际上是在告诉它循环遍历列表中的 5000 个项目,因此 list index out of range
。你真的只是希望它循环遍历每个元素,直到它用完所有项目。你可以简单地说 for url in link:
然后你的另一个嵌套 for 循环也是如此。
变化:
j = 0
for j in range (0, linko):
curl = linkia[j]
至:
for curl in linkia:
我还要注意,如果您按照自己的方式设置它,则不需要将初始 i 或 j 设置为 = 0。因为您将 range/list 设置为从 0、5000 开始...for 循环会自动从 0 的第一个元素开始。但同样,那一点是无关紧要的,因为我不建议像那样遍历你的列表。它 a) 不健壮(每次进入该循环时您的列表中都需要恰好 5000 个项目),并且 b) 虽然它对您的第二个循环工作正常,因为您将范围设置为从 0 到列表,它确实是不必要的,因为您可以将其压缩为 1 行。
尝试:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
import csv
my_url = "http://www.example.com"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
links = page_soup.select('dt > a[href]')
link = [tag.get('href') for tag in links]
for url in link:
Client = uReq(url)
pageHtml = Client.read()
Client.close()
pSoup = soup(pageHtml, "html.parser")
linkeas = pSoup.findAll(href=re.compile(my_url))
def linkas(href):
return href and re.compile("html").search(href) and re.compile(my_url).search(href)
linka = pSoup.findAll(href=linkas)
if linka != []:
linkia = [tag.get('href') for tag in linka]
for curl in linkia:
cClient = uReq(curl)
pageHtml = cClient.read()
cClient.close()
Soup = soup(page_html, "html.parser")
country = Soup.select('.class > a:nth-of-type(3)')
countri = country[0].text.strip()
print(countri)