특정 항목에 대해 Ebay에서 판매 된 모든 목록의 데이터를 가져 오려고합니다. 나는 페이지의 첫 번째 항목을 잡아하는 방법을 알아 냈하지만 난 이후의 모든 게시물을 잡아 for 루프를 만들려고 할 때, 내가 시도하고을의 바로 그 때, 부분이 잘 작동오류가있는 for 루프를 작성할 때마다 데이터 스크 레이퍼를 통해 하나의 목록에서 데이터를 가져올 수 있음
from bs4 import BeautifulSoup
import requests
source = requests.get('https://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=charmander%201st%20edition%20psa%2010&LH_Complete=1&LH_Sold=1&rt=nc&_trksid=p2045573.m1684').text
soup = BeautifulSoup(source, 'lxml').find('w-root')
#This gives the HTML of the page that includes all the listings
item = soup.li
#each specific item is in an li tag and this variable grabs the first item that was sold
title = item.h3.a.text
print(title)
price = item.span.text
print(price)
#correctly prints out the Title and price of the first listing
실패 루프가 다른 모든 목록을 가져 오면 오류가 발생합니다.
for li in soup:
title = li
print(title)
#this gives me the tags for each listing but when i try and go deeper into the tags like...
for li in soup:
title = li.h3.a.text #similar to how i originally grabbed the first item
print(title)
it prints out: "AttributeError: 'NavigableString' object has no attribute 'h3'"
과 마찬가지로 가격 변수 : 여기 몇 가지 다른 일을 시도했지만 한
은 예입니다. 심지어이 오류가 발생하지 않고 li.prettify()
을 수행 할 수 없습니다.
내가 나쁜 접근법을 사용하고 있거나 누군가이 문제를 해결하는 방법을 알고 있다면 크게 감사 할 것입니다. 감사합니다
감사합니다. 그건 나에게 많은 의미가 있습니다. 나는 코딩에 관한 기초를 배우고 있는데, 나는 이것을 시도해 볼 수 있어야한다고 생각한다. 그러나이 코드를 실행하면 첫 번째 제목 만 인쇄되고 프로그램이 중지됩니다. 내가 좋아하는 모든 타이틀을 파싱하지 않는 이유는 무엇입니까? – Gmuersch
흠, 그 중 하나는 작동하지 않는 것 같습니다. 내가 쓸 때 수프 = BeautifulSoup (소스, 'lxml'). 찾기 ('w-root') 대신 find ('w-root') 'ul'class_ = ListVIewInner를 사용할 수있는 방법이 있습니까? – Gmuersch
@Gmuersch 모든 항목의 가격을 얻을 수있는 코드를 내 대답에 추가했습니다. 그것을 시도하십시오 –