Python 美化组循环不是';t遍历其他节点
在这方面也有类似的情况,;但我一直在和别人比较。 等等,但不知何故;我不确定为什么我的Python 美化组循环不是';t遍历其他节点,python,beautifulsoup,Python,Beautifulsoup,在这方面也有类似的情况,;但我一直在和别人比较。 等等,但不知何故;我不确定为什么我的for循环没有迭代并从其他元素获取文本,而只是从节点的第一个元素获取文本 from requests import get from bs4 import BeautifulSoup url = 'https://shopee.com.my/' l = [] headers = {'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)'}
for循环
没有迭代并从其他元素获取文本,而只是从节点的第一个元素获取文本
from requests import get
from bs4 import BeautifulSoup
url = 'https://shopee.com.my/'
l = []
headers = {'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)'}
response = get(url, headers=headers)
html_soup = BeautifulSoup(response.text, 'html.parser')
def findDiv():
try:
for container in html_soup.find_all('div', {'class': 'section-trending-search-list'}):
topic = container.select_one(
'div._1waRmo')
if topic:
print(1)
d = {
'Titles': topic.text.replace("\n", "")}
print(2)
l.append(d)
return d
except:
d = None
findDiv()
print(l)
试试这个:
toplevel正在查找选项的根,然后我们将查找该选项下的所有div。
我希望这就是你想要的
from requests import get
from bs4 import BeautifulSoup
url = 'https://shopee.com.my/'
l = []
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'}
response = get(url, headers=headers)
html_soup = BeautifulSoup(response.text, 'html.parser')
def findDiv():
try:
toplevel = html_soup.find('._25qBG5')
for container in toplevel.find_all('div'):
topic = container.select_one('._1waRmo')
if topic:
print(1)
d = {'Titles': topic.text.replace("\n", "")}
print(2)
l.append(d)
return d
except:
d = None
findDiv()
print(l)
这可以很好地枚举本地文件。当我尝试使用给定的url时,网站没有返回您显示的html
from requests import get
from bs4 import BeautifulSoup
url = 'path_in_here\\test.html'
l = []
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'}
example = open(url,"r")
text = example.read()
#response = get(url, headers=headers)
#html_soup = BeautifulSoup(response.text, 'html.parser')
html_soup = BeautifulSoup(text, 'html.parser')
print (text)
def findDiv():
#try:
print("finding toplevel")
toplevel = html_soup.find("div", { "class": "_25qBG5"} )
print ("found toplevel")
divs = toplevel.findChildren("div", recursive=True)
print("found divs")
for container in divs:
print ("loop")
topic = container.select_one('.1waRmo')
if topic:
print(1)
d = {'Titles': topic.text.replace("\n", "")}
print(2)
l.append(d)
return d
#except:
# d = None
# print ("error")
findDiv()
print(l)
输出:
[{'Titles': 'school backpack'}, {'Titles': 'oppo case'}, {'Titles': 'baby chair'}, {'Titles': 'car holder'}, {'Titles': 'sling beg'}]
我再次建议你使用。如果再次运行此操作,您将看到列表中会出现一组不同的5个字典。每次你提出请求时,他们都会给出5个随机趋势项。但它们确实有一个“改变”按钮。如果您使用selenium,您可能只需单击它并继续删除所有趋势项。这行内容不应该是:topic=container。选择一个('.\u 1waRmo')-换句话说,只需类名。另外,行html_soup.find_all('div',{'class':'section trending search list'})将只找到根元素,您不需要使用html_soup.find_all('div')来枚举所有div。或者,如果您想枚举div类_25qBG5下的所有内容,请查找该类(称为toplevel,然后是options=toplevel.find('div'),然后是options中的option。
toplevel=html_soup.find('._25qBG5'))
遗憾地返回一个空值。这是我正在寻找的,我理解它的概念;但不知何故,当我调用它时,它返回了None
。我在标题上的错误,因为我忘记了重新编辑标题以包含“用户代理”:“Googlebot/2.1(+http://www.google.com/bot.html)
@Minial我将删除我的答案。并提出正确的解决方案。感谢您的推荐,我可能会找到更多关于Selenium的信息,因为我相信它将有助于我在未来解决很多问题。
[{'Titles': 'school backpack'}, {'Titles': 'oppo case'}, {'Titles': 'baby chair'}, {'Titles': 'car holder'}, {'Titles': 'sling beg'}]