实际上,我对Python的语法分析很在行。我在刮本网站。我需要头版的每英里当前价格。
我已经用了三个小时了。在网上寻找解决方案的时候。我了解到,有一个库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'))请看一下这张照片。我认为问题在于::前和:后标记。一旦加载页面,它们就会出现。
任何帮助都将不胜感激。
发布于 2019-03-24 23:20:13
您应该使用selenium而不是“请求”:
from selenium import webdriver
from bs4 import BeautifulSoup
def parse(url):
driver = webdriver.Chrome('D:\Programming\utilities\chromedriver.exe')
driver.get('https://boglagold.com/buy-runescape-gold/')
soup = BeautifulSoup(driver.page_source)
return soup.find('h4', {'id': 'curr-price-per-mil-text'}).text
parse()输出:
'Current Price Per Mil: 0.80USD'原因是该元素的值是通过JavaScript获得的,requests无法处理。这个特定的代码片段使用Chrome驱动程序;如果您愿意,可以使用Firefox/其他类似浏览器(您需要安装selenium库并自己寻找Chrome驱动程序)。
发布于 2019-03-25 00:09:13
该网页生成一个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发布于 2019-03-25 00:21:15
正如其他答案所提到的,此页面仅包含文本Current Price Per Mil:和0USD。中间0.8 -中的值是通过JS从下面描述的url动态获得的(可以获得使用这里和许多其他地方描述的过程(例如) )。该网站检查机器人,所以你有使用这里描述的方法(例如)。
所以,所有这些都是:
url = 'https://api.boglagold.com/api/product/?id=osrs-gold&couponCode=null'
import requests
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
response.json()['sellPrice']输出:
0.8https://stackoverflow.com/questions/55329489
复制相似问题