2016-10-02 4 views
0

Als Übung versuche ich Drucken alle Titel von Beiträgen mit mehr als 200 Kommentare von der Website reddit.com.Wie bekomme ich eine Liste von zwei verschiedenen Elementen mit ihren Eltern die gleichen

Was ich versuchte:

import requests 
from bs4 import BeautifulSoup 


url1 = "https://www.reddit.com/" 
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} 

res = requests.get(url1, headers=headers) 
res.raise_for_status() 
soup = BeautifulSoup(res.content, "html5lib") 

g = soup.select('ul > li.first') 
j = soup.select('#siteTable div.entry.unvoted > p.title > a ') 
list1 = [] 
for t in j: 
    list.append(t.text) 

list2=[] 
for s in g: 
    for p in s.text.split(" "): 
     if p.isdigit(): 
      p = int(p) 
      if p > 100: 
       list2.append(p) 

for q,l in zip(list1, list2): 
    if l > 200: 
     print(q,l) 

Problem:

Es funktioniert auf halbem Weg, bis es einen Schluckauf irgendwo ist und die Listen stimmen nicht mehr überein. Als Ergebnis bekomme ich Titel mit weniger als 200 Kommentaren.

Ausgang:

What the F David Blaine!! 789 
So NYC MTA (subway) banned all dogs unless the owner carries them in a bag. I think this owner nailed it. 1075 
Bad to the bone 307 
TIL there is a "white man" café in Tokyo, where Japanese ladies ring a bell to summon tuxedo-wearing caucasians who respond with "yes, princess?" and serve them cake 2145 
Earthquake Warning Issued in California 1410 
Man impersonating officer busted for attempting to pull over unmarked cruiser 1022 
Use of body-worn cameras sees complaints against police ‘virtually vanish’, study finds 2477 
Amazing one handed interception 759 
A purrfectly executed leap 518 
"This bed has a fur pillow, I'll lay here." 792 
Back in 'Nam, 1969. Guy on the left is a good friend of mine's dad. He's in hospice now and not doing well but he'll live on in photos. 264 
Nintendo Entertainment System: NES Classic Edition - with 30 games - Available in US 11/11/16 290 
A scenic view ruined by a drunk driver (Star Wars: Battlefront) 2737 
Clouds battling a sunset over Olympic National Park, WA, USA (1334x750) [OC] 2222 
What company is totally guilty of false advertising and why? 2746 
South Korean President Park Geun-hye has called on North Koreans to abandon their country and defect, just a day after a soldier walked across the heavily fortified border into the South 410 
TIFU by underestimating the stupidity of multiple people 334 
Special Trump burger at a burger chain in South Africa 311 
This Special Ed Teacher Had All of Her Students in Her Wedding 984 

Die messup beginnt nach "Eine szenische Ansicht ruiniert durch ..."

mir jemand zeigen kann, was das genaue Problem hier oder eine alternative Art und Weise?

+0

Welche in Ihrer Liste Titel weniger als 200 Kommentare hat? Eine weitere Sache, 'list.append (t.text)' sollte 'list1.append (t.text)' sein. – qmaruf

+0

Eine von einem betrunkenen Fahrer zerstörte Aussicht (Star Wars: Battlefront) >>> hatte weniger als 200 Kommentare. Sie haben recht mit list1 .... es war eigentlich korrekt in meinem Programm (beide sind Liste), aber ich wollte es hier klarer machen, also hatte ich beim Erstellen der leeren Liste in list1 gewechselt, aber vergessen Sie auch nicht, weiter unten zu ändern – BitByBit

Antwort

0

Anstatt es zuerst in Listen zu speichern und zu hoffen, dass beide Listen übereinstimmen (list1 [0] ~~ list2 [0]) .... Ich habe versucht, den häufigsten Nenner (Eltern) zu finden und die Klasse angewendet Auswahl (beautifulsoup) ein zweites Mal, um tiefer in die dom (Kinder) zu tauchen und es sofort zu drucken. Wenn Sie eine stark frequentierte Website wie Reddit scrapen, ist es eine gute Möglichkeit, selbst wenn sie in Sekundenbruchteilen auseinandergeht, eine tragfähige Ursache für Schluckauf zu sein, wenn Sie Daten in Listen speichern und vergleichen.

Lösung:

import requests 
from bs4 import BeautifulSoup 


url1 = "https://www.reddit.com/" 
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} 

res = requests.get(url1, headers=headers) 
res.raise_for_status() 
soup = BeautifulSoup(res.content, "html5lib") 

k= soup.select('#siteTable div.entry.unvoted') # partent 

for v in k: 
    d = v.select('ul > li.first') #comment 
    o = v.select('p.title > a') #title 
    for z,x in zip(d,o): 
     for p in z.text.split(" "): # convert "351 comments" to integer "351" and compare with 200 
      if p.isdigit(): 
       p = int(p) 
       if p > 200: 
        print(z.text, x.text) #print comments first then title 
Verwandte Themen