안녕하세요 저는 파이썬으로 요소를 스크랩하는 방법을 배우려고하는데 웹 페이지 (local.ch)의 제목을 얻으려고했지만 코드가 작동하지 않고 이유를 모르겠습니다.
여기에 파이썬 코드 :
import requests
from bs4 import BeautifulSoup
def spider(max_pages):
page = 2
while page < max_pages:
url = 'http://yellow.local.ch/fr/q/Morges/Bar.html?page=' + str(page)
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.findAll('a', {'class':'details-entry-title-link'}):
title = link.string
print(title)
page += 1
spider(3)
코드가 정확하다고 확신합니다. pycharm에 오류가 없습니다. 왜 작동하지 않습니까?
코드에 주요 버그가 있습니다.
page = 1
while page < max_pages
....
spider(1)
조건이 충족되지 않고 나머지 코드가 실행되지 않습니다! 다른 버그는 인코딩 오류 및 지정되지 않은 파서 경고입니다.
import requests
from bs4 import BeautifulSoup
def spider(max_pages):
page = 1
while page <= max_pages:
url = 'http://yellow.local.ch/fr/q/Morges/Bar.html?page=' + str(page)
source_code = requests.get(url)
plain_text = source_code.text.encode("utf-8")
soup = BeautifulSoup(plain_text, 'html.parser')
for link in soup.findAll('a', {'class':'details-entry-title-link'}):
title = link.string
print(title.encode("utf-8"))
page += 1
spider(1)
인코딩 "utf-8"
부분에 유의하십시오. 이 인코딩은 b
접두사 에서 볼 수 있듯이 이진 출력이됩니다 . 이 단계가 없으면 print()
함수에서 오류가 발생합니다. plain_textplain_text = source_code.text.encode("utf-8")
온라인에서도 동일한 변경이 이루어집니다 .
또 다른 버그는 잘못된 page += 1
줄 들여 쓰기입니다 . while 루프 안에 있어야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다