2013-03-20 76 views
9

Tôi phải viết trình thu thập dữ liệu web bằng Python. Tôi không biết cách phân tích một trang và trích xuất các URL từ HTML. Tôi nên đi đâu và học viết một chương trình như vậy?Cách trích xuất URL từ một trang HTML bằng Python

Nói cách khác, có một chương trình python đơn giản có thể được sử dụng làm mẫu cho trình thu thập dữ liệu web chung không? Lý tưởng nhất là nó nên sử dụng các mô-đun tương đối đơn giản để sử dụng và nó nên bao gồm nhiều nhận xét để mô tả những gì mỗi dòng mã đang làm.

Trả lời

16

Xem mã ví dụ bên dưới. Kịch bản trích xuất mã html của một trang web (ở đây trang chủ Python) và trích xuất tất cả các liên kết trong trang đó. Hi vọng điêu nay co ich.

#!/usr/bin/env python 

import requests 
from BeautifulSoup import BeautifulSoup 

url = "http://www.python.org" 
response = requests.get(url) 
# parse html 
page = str(BeautifulSoup(response.content)) 


def getURL(page): 
    """ 

    :param page: html of web page (here: Python home page) 
    :return: urls in that page 
    """ 
    start_link = page.find("a href") 
    if start_link == -1: 
     return None, 0 
    start_quote = page.find('"', start_link) 
    end_quote = page.find('"', start_quote + 1) 
    url = page[start_quote + 1: end_quote] 
    return url, end_quote 

while True: 
    url, n = getURL(page) 
    page = page[n:] 
    if url: 
     print url 
    else: 
     break 

Output:

/ 
#left-hand-navigation 
#content-body 
/search 
/about/ 
/news/ 
/doc/ 
/download/ 
/getit/ 
/community/ 
/psf/ 
http://docs.python.org/devguide/ 
/about/help/ 
http://pypi.python.org/pypi 
/download/releases/2.7.3/ 
http://docs.python.org/2/ 
/ftp/python/2.7.3/python-2.7.3.msi 
/ftp/python/2.7.3/Python-2.7.3.tar.bz2 
/download/releases/3.3.0/ 
http://docs.python.org/3/ 
/ftp/python/3.3.0/python-3.3.0.msi 
/ftp/python/3.3.0/Python-3.3.0.tar.bz2 
/community/jobs/ 
/community/merchandise/ 
/psf/donations/ 
http://wiki.python.org/moin/Languages 
http://wiki.python.org/moin/Languages 
http://www.google.com/calendar/ical/b6v58qvojllt0i6ql654r1vh00%40group.calendar.google.com/public/basic.ics 
http://www.google.com/calendar/ical/j7gov1cmnqr9tvg14k621j7t5c%40group.calendar.google.com/public/basic.ics 
http://pycon.org/#calendar 
http://www.google.com/calendar/ical/3haig2m9msslkpf2tn1h56nn9g%40group.calendar.google.com/public/basic.ics 
http://pycon.org/#calendar 
http://www.psfmember.org 

...

3

Bạn có thể sử dụng beautifulsoup. Thực hiện theo các tài liệu và xem những gì phù hợp với yêu cầu của bạn. Tài liệu chứa đoạn mã để biết cách trích xuất URL.

from bs4 import BeautifulSoup 
soup = BeautifulSoup(html_doc) 

soup.find_all('a') # Finds all hrefs from the html doc. 
5
import sys 
import re 
import urllib2 
import urlparse 
tocrawl = set(["http://www.facebook.com/"]) 
crawled = set([]) 
keywordregex = re.compile('<meta\sname=["\']keywords["\']\scontent=["\'](.*?)["\']\s/>') 
linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>') 

while 1: 
    try: 
     crawling = tocrawl.pop() 
     print crawling 
    except KeyError: 
     raise StopIteration 
    url = urlparse.urlparse(crawling) 
    try: 
     response = urllib2.urlopen(crawling) 
    except: 
     continue 
    msg = response.read() 
    startPos = msg.find('<title>') 
    if startPos != -1: 
     endPos = msg.find('</title>', startPos+7) 
     if endPos != -1: 
      title = msg[startPos+7:endPos] 
      print title 
    keywordlist = keywordregex.findall(msg) 
    if len(keywordlist) > 0: 
     keywordlist = keywordlist[0] 
     keywordlist = keywordlist.split(", ") 
     print keywordlist 
    links = linkregex.findall(msg) 
    crawled.add(crawling) 
    for link in (links.pop(0) for _ in xrange(len(links))): 
     if link.startswith('/'): 
      link = 'http://' + url[1] + link 
     elif link.startswith('#'): 
      link = 'http://' + url[1] + url[2] + link 
     elif not link.startswith('http'): 
      link = 'http://' + url[1] + '/' + link 
     if link not in crawled: 
      tocrawl.add(link) 

tham chiếu đến: Python Web Crawler in Less Than 50 Lines (chậm hoặc không còn hoạt động, không tải cho tôi)

12

Bạn có thể sử dụng BeautifulSoup như nhiều người cũng đã nêu. Nó có thể phân tích cú pháp HTML, XML, vv Để xem một số tính năng của nó, hãy xem here.

Ví dụ:

import urllib2 
from bs4 import BeautifulSoup 
url = 'http://www.google.co.in/' 

conn = urllib2.urlopen(url) 
html = conn.read() 

soup = BeautifulSoup(html) 
links = soup.find_all('a') 

for tag in links: 
    link = tag.get('href',None) 
    if link is not None: 
     print link 
Các vấn đề liên quan