编程语言
首页 > 编程语言> > python-使用beautifulsoup4进行抓取时数据丢失

python-使用beautifulsoup4进行抓取时数据丢失

作者:互联网

实际上,我是使用Python Beautifulsoup4进行解析的新手.我正在抓取this website.我需要在首页上显示“当前每百万价格”.

我已经花了3个小时了.在互联网上寻找解决方案时.我知道有一个PyQT4库,它可以像Web浏览器一样模拟并加载内容,然后在完成加载后就可以提取所需的数据.但是我坠毁了.

使用此方法以原始文本格式收集数据.我也尝试了其他方法.

def parseMe(url):
    soup = getContent(url)
    source_code = requests.get(url)
    plaint_text = source_code.text
    soup = BeautifulSoup(plaint_text, 'html.parser')
    osrs_text = soup.find('div', class_='col-md-12 text-center')
    print(osrs_text.encode('utf-8'))

Please have a look on this image.我认为问题在于:: before和:: after标记.页面加载后它们就会出现.我们将不胜感激任何帮助.

解决方法:

该网页使XHR可以获取其中包含价格的JSON文件

import requests

r = requests.get('https://api.boglagold.com/api/product/?id=osrs-gold&couponCode=null')
j = r.json()
# print(j)
print('sellPrice', j['sellPrice'])
print('buyPrice', j['buyPrice'])

输出:

sellPrice 0.8
buyPrice 0.62

标签:python-3-x,beautifulsoup,python-requests,web-scraping,python
来源: https://codeday.me/bug/20191211/2105529.html