Ahm*_*mad 3 python beautifulsoup web-scraping
嗨,我关注并理解了这篇关于如何从站点读取内容的文章,它运行良好:geeksforgeeks.org : Reading selected web content using Python Web Scraping
但是当我更改我的代码以与另一个站点一起使用时,它不会返回任何值。我正在尝试获取那些 Value1 和 Value2 等,如下所示。
请注意:从该网页阅读内容是合法的。
import requests
from bs4 import BeautifulSoup
# the target we want to open
url='https://hackerone.com/directory?offers_bounties=true&asset_type=URL&order_direction=DESC&order_field=started_accepting_at'
#open with GET method
resp=requests.get(url)
#http_respone 200 means OK status
if resp.status_code==200:
print("Successfully opened the web page")
print("The news are as follow :-\n")
# we need a parser,Python built-in HTML parser is enough .
soup=BeautifulSoup(resp.text,'html.parser')
# l is the list which contains all the text i.e news
l=soup.find("tr","spec-directory-entry daisy-table__row fade fade--show")
#now we want to print only the text part of the anchor.
#find all the elements of a, i.e anchor
for i in l:
print(i.text)
else:
print("Error")
Run Code Online (Sandbox Code Playgroud)
这是网站源代码:
<tr class="spec-directory-entry daisy-table__row fade fade--show">
<a href="/livestream" class="daisy-link spec-profile-name">Value1</a>
<tr class="spec-directory-entry daisy-table__row fade fade--show">
<a href="/livestream" class="daisy-link spec-profile-name">Value2</a>
<tr class="spec-directory-entry daisy-table__row fade fade--show">
.
.
.
Run Code Online (Sandbox Code Playgroud)
JavaScript 需要呈现网页内容。使用 prerenderio 服务是一种从页面获取您要查找的数据的简单/轻松的方法。
import requests
from bs4 import BeautifulSoup
# the target we want to open
# changed to use prerenderio service
url='http://service.prerender.io/https://hackerone.com/directory?offers_bounties=true&asset_type=URL&order_direction=DESC&order_field=started_accepting_at'
#open with GET method
resp=requests.get(url)
#http_respone 200 means OK status
if resp.status_code==200:
print("Successfully opened the web page")
print("The news are as follow :-\n")
# we need a parser,Python built-in HTML parser is enough .
soup=BeautifulSoup(resp.text,'html.parser')
# l is the list which contains all the text i.e news
l=soup.find("tr","spec-directory-entry daisy-table__row fade fade--show")
#now we want to print only the text part of the anchor.
#find all the elements of a, i.e anchor
for i in l:
print(i.text)
else:
print("Error")
Run Code Online (Sandbox Code Playgroud)
上面代码返回的数据:
Successfully opened the web page
The news are as follow :-
LivestreamManaged
04 / 2019
73
$100
$150-$250
Run Code Online (Sandbox Code Playgroud)
编辑:回应艾哈迈德的评论
这是仅获取“Livestream”表行值的代码。
Successfully opened the web page
The news are as follow :-
LivestreamManaged
04 / 2019
73
$100
$150-$250
Run Code Online (Sandbox Code Playgroud)
结果:
Successfully opened the web page
The news are as follow :-
04 / 2019
73
$100
$150-$250
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
281 次 |
| 最近记录: |