【发布时间】:2020-09-30 20:50:42
【问题描述】:
我正在尝试从任何给定的维基百科页面检索所有引文数据。查看 wikipedia 页面,我需要在页面参考部分的一个范围内的 OpenURL 对象中保存很多我需要的信息。
span的格式如下:
<span
title="ctx_ver=Z39.88-2004&
rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&
rft.genre=unknown&
rft.jtitle=The+Tennessean&
rft.atitle=Belmont+University+awarded+final+2020+presidential+debate&
rft.date=2019-10-11&
rft.aulast=Tamburin&
rft.aufirst=Adam&
rft_id=https%3A%2F%2Fwww.tennessean.com%2Fstory%2Fnews%2F2019%2F10%2F11%2Fbelmont-university-nashville-hosts-presidential-debate-2020%2F3941983002%2F&
rfr_id=info%3Asid%2Fen.wikipedia.org%3A2020+United+States+presidential+election"
class="Z3988">
</span>
到目前为止,我已经能够使用 beautifulSoup 检索所有 span 并提取包含数据的标题。但是,在解析 title 字段中的文本时,我感到很困惑。我对rft.atitle、rft.date和rft_id特别感兴趣
import requests
from bs4 import BeautifulSoup
session = requests.Session()
targetWikiPage = "https://en.wikipedia.org/wiki/2020_Beirut_explosion"
if "wikipedia" in targetWikiPage:
html = session.post(targetWikiPage)
bsObj = BeautifulSoup(html.text, "html.parser")
html = session.post(targetWikiPage)
bsObj = BeautifulSoup(html.text, "html.parser")
wikiReferences = bsObj.find_all('span', {'class': 'Z3988'})
wikiReferencesBS = BeautifulSoup(str(wikiReferences), "html.parser")
for span in wikiReferencesBS.find_all():
title = span['title']
print(title)
部分解决方案
此解决方案提供了一个接受字符串和两个标志的函数。我们要解析的字符串的开头和结束标志的第一个实例的结尾。
我现在面临的问题是unboundLocalError
Traceback (most recent call last):
File "coinscraper.py", line 33, in <module>
print(extractstring(title,flag1='rft.atitle=', flag2='&'))
File "coinscraper.py", line 17, in extractstring
return(string)
UnboundLocalError: local variable 'string' referenced before assignment
修改
import requests
from bs4 import BeautifulSoup
import re
session = requests.Session()
targetWikiPage = "https://en.wikipedia.org/wiki/2020_Beirut_explosion"
def extractstring(line,flag1, flag2):
if flag1 in line: # $ is the flag
dex1=line.index(flag1)
subline=line[dex1+len(flag1):-1] #leave out flag (+1) to end of line
dex2=subline.index(flag2)
string=subline[0:dex2].strip() #does not include last flag, strip whitespace
string = urllib.parse.unquote_plus(string)
return(string)
if "wikipedia" in targetWikiPage:
html = session.post(targetWikiPage)
bsObj = BeautifulSoup(html.text, "html.parser")
html = session.post(targetWikiPage)
bsObj = BeautifulSoup(html.text, "html.parser")
wikiReferences = bsObj.find_all('span', {'class': 'Z3988'})
wikiReferencesBS = BeautifulSoup(str(wikiReferences), "html.parser")
for span in wikiReferencesBS.find_all():
title = span['title']
print(extractstring(title,flag1='rft.atitle=', flag2='&'))
【问题讨论】: