我试图建立使用Python和BeautifulSoup进入eBay的精选集和检索的集合中的所有产品的URL(最有收藏17个产品网页抓取工具,虽然有些还多或少)。下面是我试图在我的代码,以刮擦收集的网址:http://www.ebay.com/cln/ebayhomeeditor/Surface-Study/324079803018刮易趣特色产品页面集的链接
这里是我到目前为止的代码:
import requests
from bs4 import BeautifulSoup
url = 'http://www.ebay.com/cln/ebayhomeeditor/Surface-Study/324079803018'
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
product_links = []
item_thumb = soup.find_all('div', attrs={'class':'itemThumb'})
for link in item_thumb:
product_links.append(link.find('a').get('href'))
print product_links
这应该刮追加17个链接到列表中product_links。但是,它仅适用于中途。具体来说,它只会每次删除前12个产品链接,剩下的5个链接不变,即使所有17个链接都在相同的HTML标签和属性中找到。在网页的HTML代码更仔细地观察,我发现唯一的区别是,第12个环节,最终5是由一块XML脚本的,我已经在这里包括分离:
<script escape-xml="true">
if (typeof(collectionState) != 'object') {
var collectionState = {
itemImageSize: {sWidth: 280, sHeight: 280, lWidth: 580, lHeight: 620},
page: 1,
totalPages: 2,
totalItems: 17,
pageId: '2057253',
currentUser: '',
collectionId: '323101965012',
serviceHost: 'svcs.ebay.com/buying/collections/v1',
owner: 'ebaytecheditor',
csrfToken: '',
localeId: 'en-US',
siteId: 'EBAY-US',
countryId: 'US',
collectionCosEnabled: 'true',
collectionCosHostExternal: 'https://api.ebay.com/social/collection/v1',
collectionCosEditEnabled: 'true',
isCollectionReorderEnabled: 'false',
isOwnerSignedIn: false || false,
partiallySignedInUser: '@@[email protected]@[email protected]@',
baseDomain: 'ebay.com',
currentDomain: 'www.ebay.com',
isTablet: false,
isMobile: false,
showViewCount: true
};
}
</script>
什么是功能这个脚本?这个剧本有可能是我的刮刀忽略了最后5个链接的原因吗?有没有办法解决这个问题并为最后的五个方案做好准备?
发生这种情况,因为在接下来的5个环节是使用JavaScript加载。 –