Webscraper,将列表提取到.json?
我正试图在一个网站(www.cashpoint.dk)上搜寻赔率和其他相关信息 如果我想提取数据的输出,有人能帮我做些什么吗 打印((bet['team1']、bet['team2']、bet['bettext']、bet['tiptext']、bet['tip'])到一个.json文件,并包含所有赔率和文本 我希望这个结构对您有意义,如果代码看起来不好,我很抱歉,我对python和一般的编码都是新手Webscraper,将列表提取到.json?,json,python-3.x,Json,Python 3.x,我正试图在一个网站(www.cashpoint.dk)上搜寻赔率和其他相关信息 如果我想提取数据的输出,有人能帮我做些什么吗 打印((bet['team1']、bet['team2']、bet['bettext']、bet['tiptext']、bet['tip'])到一个.json文件,并包含所有赔率和文本 我希望这个结构对您有意义,如果代码看起来不好,我很抱歉,我对python和一般的编码都是新手 import demjson import json import io import re
import demjson
import json
import io
import re
from bs4 import BeautifulSoup
import requests
url = "https://www.cashpoint.dk/en/?r=bets/xtra&group=461392&game=312004790"
print(url)
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
class Scraper():
def __init__(self):
self.tables = soup.select('table.sportbet_extra_list_table')
for table in self.tables:
self.fields = table.select('.sportbet_extra_rate_content')
for field in self.fields:
self.js_obj = re.search('{.+}', field['onclick']).group()
self.bet = demjson.decode(self.js_obj)
print((self.bet['team1'], self.bet['team2'], self.bet['bettext'], self.bet['tiptext'], self.bet['tip']))
def parseJSON(self):
try:
self.to_unicode = unicode
except NameError:
self.to_unicode = str
# Define data
self.data = {
'dictionary:': {
'tip': str(self.bet['tip']),
'team1': str(self.bet['team1']),
'team2': str(self.bet['team2']),
'bettext': str(self.bet['bettext']),
'odds': str(self.bet['odd']),
}
}
# Write JSON file
with io.open('data.json', 'w', encoding='utf8') as outfile:
self.str_ = json.dumps(self.data,
indent=4, sort_keys=True,
separators=(',', ': '), ensure_ascii=True)
outfile.write(self.to_unicode(self.str_))
# Read JSON file
with open('data.json') as data_file:
self.data_loaded = json.load(data_file)
print(self.data == self.data_loaded)
Scraper()
我正在尝试提取的HTML代码:
维姆·维德·坎彭?
1.
1,38
也许你应该看看,这个工具允许你通过创建一个解析器(就像你做的那样)来废弃网站,并将创建的项目导出到你想要的格式
您必须创建一个爬行器
和一个项目
来存储您的信息,然后使用以下工具运行爬网:
scrapy crawl'your_parser_name'-t json-o'/path/to/your/file.json'
我想从头开始创建自己的解析器,因为我主要学习它的工作原理。