4
웹 사이트를 긁을 수 있도록 python 3.x를 배우려고합니다. 사람들은 Beautiful Soup 4 또는 lxml.html을 사용하도록 권장했습니다. 누군가 파이썬 3.x와 함께 BeautifulSoup에 대한 튜토리얼이나 예제를 올바른 방향으로 가르쳐 주시겠습니까?파이썬 3을 사용하는 웹 스크래핑 자습서?
도움 주셔서 감사합니다.
웹 사이트를 긁을 수 있도록 python 3.x를 배우려고합니다. 사람들은 Beautiful Soup 4 또는 lxml.html을 사용하도록 권장했습니다. 누군가 파이썬 3.x와 함께 BeautifulSoup에 대한 튜토리얼이나 예제를 올바른 방향으로 가르쳐 주시겠습니까?파이썬 3을 사용하는 웹 스크래핑 자습서?
도움 주셔서 감사합니다.
실제로 파이썬에 몇 가지 샘플 코드가 포함 된 a full guide on web scraping을 작성했습니다. 파이썬 2.7에서 작성하고 테스트했지만 두 가지 패키지 (요청과 BeautifulSoup)는 모두 Wall of Shame에 따라 파이썬 3과 완벽하게 호환됩니다.
다음import sys
import requests
from BeautifulSoup import BeautifulSoup
def scrape_google(keyword):
# dynamically build the URL that we'll be making a request to
url = "http://www.google.com/search?q={term}".format(
term=keyword.strip().replace(" ", "+"),
)
# spoof some headers so the request appears to be coming from a browser, not a bot
headers = {
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"accept-encoding": "gzip,deflate,sdch",
"accept-language": "en-US,en;q=0.8",
}
# make the request to the search url, passing in the the spoofed headers.
r = requests.get(url, headers=headers) # assign the response to a variable r
# check the status code of the response to make sure the request went well
if r.status_code != 200:
print("request denied")
return
else:
print("scraping " + url)
# convert the plaintext HTML markup into a DOM-like structure that we can search
soup = BeautifulSoup(r.text)
# each result is an <li> element with class="g" this is our wrapper
results = soup.findAll("li", "g")
# iterate over each of the result wrapper elements
for result in results:
# the main link is an <h3> element with class="r"
result_anchor = result.find("h3", "r").find("a")
# print out each link in the results
print(result_anchor.contents)
if __name__ == "__main__":
# you can pass in a keyword to search for when you run the script
# be default, we'll search for the "web scraping" keyword
try:
keyword = sys.argv[1]
except IndexError:
keyword = "web scraping"
scrape_google(keyword)
그냥 일반적으로 파이썬 3에 대해 자세히 알아 보려면 이미 파이썬 2.x를 잘 알고있는 경우, this article 전환에 : 여기
는 웹 파이썬에서 긁어를 시작하는 몇 가지 코드입니다 Python 2부터 Python 3까지 도움이 될 것입니다.
웹 스크래핑을 원한다면 Python 2를 사용하십시오. [Scrapy] (http://doc.scrapy.org/en/latest/intro/tutorial.html)는 파이썬을위한 최고의 웹 스크래핑 프레임 워크이며 3.x는 없습니다. – Blender