【问题标题】:I want to get all links from a certain webpage using python我想使用 python 从某个网页获取所有链接
【发布时间】:2017-06-06 00:29:01
【问题描述】:
我希望能够使用 python https://yeezysupply.com/pages/all 从以下网页中提取所有 url,我尝试使用我找到的其他一些建议,但它们似乎不适用于这个特定的网站。我最终会找不到任何网址。
import urllib
import lxml.html
connection = urllib.urlopen('https://yeezysupply.com/pages/all')
dom = lxml.html.fromstring(connection.read())
for link in dom.xpath('//a/@href'):
print link
【问题讨论】:
标签:
python
parsing
shopify
【解决方案1】:
也许使用专门为此设计的模块会对您有用。这是一个快速而肮脏的脚本,可以获取页面上的相对链接
#!/usr/bin/python3
import requests, bs4
res = requests.get('https://yeezysupply.com/pages/all')
soup = bs4.BeautifulSoup(res.text,'html.parser')
links = soup.find_all('a')
for link in links:
print(link.attrs['href'])
它会产生这样的输出:
/pages/jewelry
/pages/clothing
/pages/footwear
/pages/all
/cart
/products/womens-boucle-dress-bleach/?back=%2Fpages%2Fall
/products/double-sleeve-sweatshirt-bleach/?back=%2Fpages%2Fall
/products/boxy-fit-zip-up-hoodie-light-sand/?back=%2Fpages%2Fall
/products/womens-boucle-skirt-cream/?back=%2Fpages%2Fall
etc...
这就是你要找的吗?请求和漂亮的汤是非常棒的抓取工具。
【解决方案2】:
页面源中没有链接;它们是在页面加载到浏览器后使用 Javascript 插入的。