我有一些如下网址:
imsges =
<img class="wni-logo" src="https://smtgvs.weathernews.jp/s/topics/img/wnilogo_kana@2x.png"/>
<img alt="top" id="top_img" src="//smtgvs.weathernews.jp/s/topics/img/201808/201808170115_top_img_A.jpg?1534474260" style="width: 100%;"/>
<img alt="box0" id="box_img0" src="//smtgvs.weathernews.jp/s/topics/img/201808/201808170115_box_img0_A.png?1534474573" style="width:100%"/>
<img alt="box1" class="lazy" data-original="https://smtgvs.weathernews.jp" id="box_img1" src="https://smtgvs.weathernews.jp/s/topics/img/dummy.png" style="width: 100%; display: none;"/>
<img alt="recommend thumb0" height="70" src="https://smtgvs.weathernews.jp/s/topics/thumb/article/201808080245_top_img_A_320x240.jpg?1534473603" width="100px"/>
我想得到如下结果:
['https://smtgvs.weathernews.jp/s/topics/img/201808/201808170115_top_img_A.jpg']
['https://smtgvs.weathernews.jp/s/topics/img/201808/201808170115_box_img0_A.png']
我尝试了以下代码:
for image in images:
imageURL = re.findall('https://smtgvs.weathernews.jp/s/topics/img/.+', urljoin(baseURL, image['src']))
if imageURL:
print(imageURL)
我得到了那些结果,您能为我更正吗?
['https://smtgvs.weathernews.jp/s/topics/img/201808/201808170115_top_img_A.jpg?1534474260']
['https://smtgvs.weathernews.jp/s/topics/img/201808/201808170115_box_img0_A.jpg?1534474573']
['https://smtgvs.weathernews.jp/s/topics/img/dummy.png']
答案 0 :(得分:1)
您可以使用捕获组直接更改正则表达式
for image in images:
imageURL = re.findall("(https://smtgvs.weathernews.jp/s/topics/img/[0-9]+/.+)\?[0-9]+", urljoin(baseURL, image['src']))
if imageURL:
print(imageURL)
编辑:获取原始数据而不是src字段:
soup = BeautifulSoup(html_doc, 'html.parser')
for image in soup.find_all("img"):
print(image.get("data-original"))