我正在尝试使用请求从网页中获取一些数字。图像中有可用的数字。到目前为止,我编写的脚本可以show
像使用PIL
库一样显示数字,但不能打印它们。
提交按钮上方的可见数字如下:
到目前为止,我已经尝试过:
import io
import requests
from PIL import Image
from bs4 import BeautifulSoup
from urllib.parse import urljoin
base = 'http://horoscope.horoscopezen.com/'
url = 'http://horoscope.horoscopezen.com/archive2.asp?day=2&month=1&year=2022&sign=1#.Xy07M4oza1v'
def get_numbers(link):
r = requests.get(link)
soup = BeautifulSoup(r.text,"lxml")
image_links = [urljoin(base,td['src']) for td in soup.select("td > img[src^='secimage.asp?']")]
for image_link in image_links:
r = requests.get(image_link)
img = Image.open(io.BytesIO(r.content))
img.show()
break
if __name__ == '__main__':
get_numbers(url)
我如何从该站点获取号码?
您无需在此处使用OCR。图像本身由每个数字的单独图像组成,并且通过解析图像链接,您可以获得完整的数字。图像链接的格式如下:http://horoscope.horoscopezen.com/secimage.asp?I=1&N=595A5C585A5C
看来I=
参数是数字的索引,而N=
参数是整数。翻译似乎如下:
56 -> 9
57 -> 8
58 -> 7
59 -> 6
5A -> 5
5B -> 4
5C -> 3
5D -> 2
5E -> 1
5F -> 0
请注意,这些数字采用十六进制编码(所有字符均为0-9,AF)。由于0x56对应于9,而0x5F对应于0(并且0x56 + 9 == 0x5F),因此我们可以使用公式获得数字9 - hex_num + 0x56
。例如,将56转换为9 - 0x56 + 0x56 = 9
,将5E转换为9 - 0x5E + 0x56 = 9 - 8 = 1
因此,您可以使用以下方式更改代码以打印出完整的数字:
def url_to_number(url):
all_digits = []
# We want the encoded number, find '&N=' and get the characters after it
N = url[url.find('&N=') + 3:]
# loop the characters in pairs
for i in range(0, len(N), 2):
digit = 9 - int(N[i:i+2], 16) + 0x56
all_digits.append(digit)
return all_digits
该行digit = 9 - int(N[i:i+2], 16) + 0x56
完成了我前面提到的转换。int(N[i:i+2], 16)
将数字从字符串转换为整数(以16为基数(十六进制))。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句