构建一个python web scraper,需要帮助来获得正确的输出

2024-09-30 14:31:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用python构建一个web刮板。 我的scraper的目的是从这个网页获取所有指向网站的链接http://www.ebizmba.com/articles/torrent-websites

我想要的输出是-

www.thepiratebay.se
www.kat.ph

我是python和scraping的新手,我这么做只是为了练习。请帮助我得到正确的输出。在

我的代码---------------------------------

^{pr2}$

我的输出---http://i.stack.imgur.com/Xi37B.png


Tags: 目的刮板comwebhttp网页网站链接
2条回答

像这样使用^{}

import requests    
from bs4 import BeautifulSoup

r = requests.get("http://www.ebizmba.com/articles/torrent-websites")

soup = BeautifulSoup(r.text, "html.parser")
data = soup.find_all("div", {"class:", "main-container-2"})

for i in data:
    for j in i.contents[1].find_all("a"):
        print(j.get('href'))

全输出:

^{pr2}$

如果你在网上练习,看看正则表达式。 这里只会得到标题链接。。。针串是匹配字符串,括号(http://.*?)包含匹配组。在

import urllib2
import re

myURL = "http://www.ebizmba.com/articles/torrent-websites"
req = urllib2.Request(myURL)

Needle1 = '<p><a href="(http:.*?)" rel="nofollow" target="_blank">'
for match in re.finditer(Needle1, urllib2.urlopen(req).read()):
   print(match.group(1))

相关问题 更多 >