使用Python刮除MediaWiki网站(特定的html标签)

时间:2018-11-24 11:26:12

标签: python python-3.x web-scraping beautifulsoup urllib

我想用特定标签刮擦这个特定的MediaWiki网站。这是我当前的代码。

import urllib.request
from bs4 import BeautifulSoup

url = "https://wiki.sa-mp.com/wiki/Strfind"
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")

# kill all script and style elements
for script in soup(["script", "style"]):
    script.extract()    # rip it out

# get text
text = soup.get_text()

# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)

print(text)

如果查看URL,则有descriptionparametersreturn valuesexample usage。那就是我要刮的东西。谢谢!

1 个答案:

答案 0 :(得分:1)

可能有一种更有效的方法来执行此操作,但是以下内容使用css选择器来获取该信息

from bs4 import BeautifulSoup
import requests as re

url ="https://wiki.sa-mp.com/wiki/Strfind"
response = re.get(url)
soup = BeautifulSoup(response.content, "lxml")

description = soup.select_one('.description').text
initial_parameters = soup.select('.parameters,.param')
final_parameters = [parameter.text for parameter in initial_parameters]
returnValues = soup.select_one('#bodyContent > .param + p + div').text
exampleUsage = soup.select_one('.pawn').text

results = [description,final_parameters,returnValues,exampleUsage]
print(results)