gpt4 book ai didi

Python - 在网站上搜索最优惠的价格

转载 作者:行者123 更新时间:2023-12-01 09:06:31 25 4
gpt4 key购买 nike

我有一堆未使用的 Steam key ,我正在尝试编写一个小型 Python 脚本来在一些最便宜的 Steam key 网站上搜索我拥有的每款游戏的最佳价格。目前,我已尝试在 g2a 上找到最优惠的价格。该脚本运行得几乎很好(是的,有时返回单个价格并不能完全返回我拥有的游戏的价格,但这不是主要问题)。问题是,只要名称的一部分包含特殊字符,我似乎就无法正确搜索网页。我尝试删除特殊字符,但问题仍然存在。有什么建议吗?

这是我的脚本:

import pandas as pd
import sys
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
import urllib.parse
carica = pd.read_csv("list.csv")
rows=0
pageh = urlopen(Request('https://www.google.com'))
while rows<len(carica)-1:

#while rows<3:
rows=rows+1
filename = (carica.values[rows,0])
filename = filename.replace(",","")
filename = filename.replace(" ","%20")
filename = filename.replace("'","")
if filename is None:filename='Demo'


quote_page = ('https://www.g2a.com/en-us/search?query='+filename)


try:page = urlopen(Request(quote_page, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30'}))
except IOError: page=pageh

soup = BeautifulSoup(page,'html.parser')


namea = carica.values[rows,0]
pricea = soup.find(class_='Card__price-cost price')
if pricea is None:pricea.text='non trovato'
testo='\x1b[0m' + carica.values[rows,0] + '\x1b[0m'
print(testo, '-->', pricea.text)

包含游戏列表的版本如下: https://repl.it/@PaoloVeronesi/g2a-prices

最佳答案

您不应该手动“引用”这些字符串 - Python 中有一组特殊的库函数可用 - quote()quote_plus()来自 urllib.parse 模块:

In [1]: from urllib.parse import quote

In [2]: s = "Small Town Terrors: Galdor's Bluff Collector's Edition"

In [3]: quote(s)
Out[3]: 'Small%20Town%20Terrors%3A%20Galdor%27s%20Bluff%20Collector%27s%20Edition'

In [4]: quote_plus(s)
Out[4]: 'Small+Town+Terrors%3A+Galdor%27s+Bluff+Collector%27s+Edition'

或者,更好的是改用 requests包只会隐式地在幕后进行引用。

关于Python - 在网站上搜索最优惠的价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com