如何显示一定数量的链接和信息
How to display a certain number of links along with information
如何显示一定数量的链接和信息?
第一个代码询问我要打开哪个页面以及要显示多少项目。但是无论我做什么,我的链接都显示正确,但文本不正确
import requests
from bs4 import BeautifulSoup
page = int(input("page: "))
results = int(input("results count: "))
URL = "..." + "page" + str(page)
HEADERS = {
"User-Agent": "..."
}
r = requests.get(url=URL, headers=HEADERS)
soup = BeautifulSoup(r.text, 'html.parser')
imgs = soup.find_all("img", class_="attachment-game size-game wp-post-image")[0:results]
data = soup.find_all("article", class_="game")[0:results]
for x in imgs:
x = x["src"]
for i in data:
i = i.text
print(x + i)
看来你的迭代逻辑是错误的。
如果你有两个列表,那么你可以使用 zip(list1, list2)
然后迭代相同的。
imgs = soup.find_all("img", class_="attachment-game size-game wp-post-image")[0:results]
data = soup.find_all("article", class_="game")[0:results]
for img, d in zip(imgs,data):
print(img["src"] + " " + d.text)
希望对您有所帮助。
如何显示一定数量的链接和信息? 第一个代码询问我要打开哪个页面以及要显示多少项目。但是无论我做什么,我的链接都显示正确,但文本不正确
import requests
from bs4 import BeautifulSoup
page = int(input("page: "))
results = int(input("results count: "))
URL = "..." + "page" + str(page)
HEADERS = {
"User-Agent": "..."
}
r = requests.get(url=URL, headers=HEADERS)
soup = BeautifulSoup(r.text, 'html.parser')
imgs = soup.find_all("img", class_="attachment-game size-game wp-post-image")[0:results]
data = soup.find_all("article", class_="game")[0:results]
for x in imgs:
x = x["src"]
for i in data:
i = i.text
print(x + i)
看来你的迭代逻辑是错误的。
如果你有两个列表,那么你可以使用 zip(list1, list2)
然后迭代相同的。
imgs = soup.find_all("img", class_="attachment-game size-game wp-post-image")[0:results]
data = soup.find_all("article", class_="game")[0:results]
for img, d in zip(imgs,data):
print(img["src"] + " " + d.text)
希望对您有所帮助。