Home > Net >  How to display a certain number of links along with information
How to display a certain number of links along with information

Time:03-25

How to display a certain number of links along with information? First code asks me which page to open and how many items to display. But no matter what I do, my links are displayed correctly, but the text is not correct

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)

CodePudding user response:

It seems your iteration logic is wrong. If you have two list you can use zip(list1, list2) and then iterate the loop.

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)

I hope this will helps. If not please share your url and expected output.

  • Related