简体   繁体   中英

python for loop is only returning first index item

As titles says everything i also looked over some stackoverflow same question but i was not able to find the solution Here is my codes

r = requests.get("http://cpaleaks.com", headers={'User-agent': 'Mozilla/5.0 (X11; Ubuntu; 
Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'})
c = r.content
soup = BeautifulSoup(c,"html.parser")

all = soup.find_all("div",{"id":"content"})

all[0].find_all("h2")[0].text

for item in all:
    print(item.find_all("h2")[0].text)

Output

6 Untapped Traffic Sources in 2019 That Convert With ANY Niche

Expected Output

Must be all titles of website

What happens?

find_all("div",{"id":"content"}) will give you a result set in your case of one <div> and its inner element tree, so you iterat only one time and print this one time only the text of first <h2>

print(item.find_all("h2")[0].text)

How to fix?

Iterate over first element in result set while find_all('h2') :

for item in all[0].find_all("h2")
    print(item.text)

or in my opinion more specific and cleaner solution, iterate over the selection of all <h2> in the element (div) with id content:

for item in soup.select('#content h2'):
    print(item.text.strip())

Example

import requests
from bs4 import BeautifulSoup
import pandas as pd
r = requests.get("http://cpaleaks.com", headers={'User-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'})
c = r.content
soup = BeautifulSoup(c,"html.parser")

for item in soup.select('#content h2'):
    print(item.text.strip())

Output

6 Untapped Traffic Sources in 2019 That Convert With ANY Niche
3 SECRET Steps to Make Money on Instagram WITHOUT Followers – Fast & Efficient
Facebook Marketing in 2019 – SMART Strategies + Case Study!
Rank Local Businesses on the FIRST page of Google – Profit from NOOBS like a PRO!
Reddit is STILL Endless Source Of FREE Marketing Traffic – IF DONE RIGHT!
How To EXPLODE Your Earnings With HOT Targeted Pins on Pinterest
The Key In the LIST! Grow Your Subscriber Count Super FAST – Build Your Money Making MACHINE!
How to Monetize Virtual Businesses on Groupon and Make Up To $100K THIS YEAR
Laser Targeted Quora Strategy to Profit With Clickbank Products – QUORA DONE RIGHT!

I am not sure how beautifulsoup works but maybe try using this code instead of the for you have at the end.

for h2 in all[0].find_all("h2")
    print(h2.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM