gpt4 book ai didi

python 正则表达式: extract special substring in a hyperlink

转载 作者:行者123 更新时间:2023-12-01 03:50:20 25 4
gpt4 key购买 nike

我使用python抓取了一系列超链接,我想从这些超链接中提取特定的字符串。超链接如下: “http://tianqi.2345.com/hongkong/61063.htm

它包含一个城市名称(hongkong)和一个城市ID(61063),我想得到以下结果:

cityName=hongkong
cityID = 61063

我的示例代码如下:

import re
reNamedGroupTestStr = 'http://tianqi.2345.com/qinxian/61063.htm'
foundTagA = re.search('http://tianqi.2345.com/(?P<CityName>.+?)/(?P<CityID>.+?).htm", reNamedGroupTestStr);
if(foundTagA):
GroupCityName = foundTagA.group("CityName");
print "CityName=",GroupCityName; #I wish to print 'hongkong'
GroupCityID = foundTagA.group("CityID");
print "CityID=",GroupCityID; #I wish to print '61063'

但是代码抛出错误,我不熟悉正则表达式,有人可以帮助我吗?

下面是我的完整代码:

# -*- coding: utf-8 -*- 
from bs4 import BeautifulSoup
import re

soup = BeautifulSoup(open("countyID.html"), "lxml")
#print(soup.prettify())
i = 0
for tag in soup.select('div.bmeta'):
if i == 5:
countys = tag
i = i + 1


for county in countys.find_all('a'):
countyid = county.get('href')
print county.get_text() #Print the city Chinese Name
print countyid[23:-10] #print the cityName
print countyid[-9:-4] #print the cityID
print '***'
#break
'''
the sample print result:
***
台北 #Print the city Chinese Name
taipei #print the cityName
71294 #print the cityID
***
'''

#test regex(corrected)
reNamedGroup = 'http://tianqi.2345.com/qinxian/61063.htm'
foundTagA = re.search('http://tianqi.2345.com/(?P<CityName>\w+?)/(?P<CityID>\d+?).htm', reNamedGroup)
if(foundTagA):
GroupCityName = foundTagA.group("CityName");
print "CityName=",GroupCityName; #I wish to print 'hongkong'
GroupCityID = foundTagA.group("CityID");
print "CityID=",GroupCityID; #I wish to print '61063'

最佳答案

您可以拆分:

u = "http://tianqi.2345.com/hongkong/61063.htm"


_, nme, c_id = u.rsplit("/", 2)
print(nme, c_id.split(".", 1)[0])

这会给你:

hongkong 61063

如果你想检查 url 是否以主机开头:

if u.startswith("http://tianqi.2345.com/"):
_, nme, c_id = u.rstrip(".htm").rsplit("/", 2)

由于您使用的是BeautifulSoup,因此您可以使用包含链接的 div 的 id 自行过滤 anchor 标记:

from bs4 import BeautifulSoup

import requests

soup = BeautifulSoup(requests.get("http://tianqi.2345.com/").content)

for a in soup.select("#hot_l a[href]"):
print(a.text)
_, nme, c_id = a["href"].rsplit("/", 2)
print(nme, c_id.split(".", 1)[0])

输出如下:

北京
beijing 54511

我无法添加所有输出,因为我收到有关垃圾邮件的警告,但它都在那里。

关于 python 正则表达式: extract special substring in a hyperlink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38344399/

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