Web Scraper的PyQuery代码

时间:2015-01-22 05:52:08

标签: python pyquery

我对python有点新手,但是我试图制作一个网络刮刀脚本,它可以在网站上下载所有图片。我使用请求和PyQuery,因为很多人在经过一些研究后推荐了它。这就是我现在所拥有的,而且我不确定该去哪里。

r = requests.get("some url")
images = pq(r.text)
for image in images.find("img"):

我知道我需要获取img的来源,但是在找到img标签后我该怎么做?此外,我已经查看了一些htmls的页面源,并且一些图片存储在他们的数据库中,因此src以" /"某些扩展"开头。所以我想知道我怎么能得到完整的网址。

1 个答案:

答案 0 :(得分:0)

(python3)

from pyquery import PyQuery as pq
import requests
from urllib.parse import urljoin

url = "..."
response = requests.get(url).text
for image in pq(response)("img") :
    imgurl = urljoin(url,image.get("src"))

在你的辩护中,the pyquery docs似乎过时了。 urllib负责将相对URL合并为绝对URL。

相关问题