- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 bs4 制作爬虫这是我要从中抓取数据的目标页面
http://sports.news.naver.com/wfootball/news/index.nhn?page=1
这是我要获取的数据
<html>~~
<head>...</head>
<body>
<some several layer...>
<div class="news_list" id="_newsList">
<h3 class="blind"> ~ </h3>
<ul>
<li>
<div class="text">
<a href="~~~~">
<span>"**targetData**"</span>
</li>
<li>
same structure
</li>
<li>...</li>
several <li>...</li>
</ul>
</div>
</layers...>
</body>
</html>
这是我的代码
#-*- coding: utf-8 -*-
import urllib
from bs4 import BeautifulSoup
targettrl = 'http://sports.news.naver.com/wfootball/news/index.nhn?page=1'
soup = BeautifulSoup(urllib.urlopen(targettrl).read(), 'html.parser')
print(soup.find_all("div", {"class":"news_list"}))
结果
[]
我该怎么办?
最佳答案
您想要的内容是使用 JavaScript 动态加载的,因此在 <div>
内的页面源代码中找不到。您正在搜索的标签。但是,数据在 <script>
内的页面源中可用。 JSON格式的标签。
你可以使用这个来抓取它:
import re
import json
import requests
r = requests.get('http://sports.news.naver.com/wfootball/news/index.nhn?page=1')
script = re.findall('newsListModel:(.*})', r.text)[0]
data = json.loads(script)
for item in data['list']:
print(item['title'])
url = 'http://sports.news.naver.com/wfootball/news/read.nhn?oid={}&aid={}'.format(item['oid'], item['aid'])
print(url)
输出:
‘로마 영웅’ 제코, “첼시 거절? 돈이 중요하지 않아”
http://sports.news.naver.com/wfootball/news/read.nhn?oid=139&aid=0002089601
손흥민, 맨시티전 선발 전망...'시즌 19호' 기대(英 매체)
http://sports.news.naver.com/wfootball/news/read.nhn?oid=139&aid=0002089600
드디어 정해진 UCL 4강 진출팀, 4강 대진은 과연?
http://sports.news.naver.com/wfootball/news/read.nhn?oid=047&aid=0002185662
극적 승리 후 ‘유물급 분수대’에 뛰어든 로마 구단주 벌금형
http://sports.news.naver.com/wfootball/news/read.nhn?oid=081&aid=0002907313
'토너먼트의 남자' 호날두, 팀 득점 6할 책임지다
http://sports.news.naver.com/wfootball/news/read.nhn?oid=216&aid=0000094120
맨유 스카우트 파견...타깃은 기성용 동료 수비수 모슨
http://sports.news.naver.com/wfootball/news/read.nhn?oid=139&aid=0002089598
이번에는 '타점', 늘 새로운 호날두의 키워드
http://sports.news.naver.com/wfootball/news/read.nhn?oid=413&aid=0000064740
승무패 14회차, 토트넘 홈에서 맨시티 이길 것
http://sports.news.naver.com/wfootball/news/read.nhn?oid=396&aid=0000477162
3부까지 추락했던 울버햄튼, 6년 만에 EPL 승격 눈앞
http://sports.news.naver.com/wfootball/news/read.nhn?oid=413&aid=0000064739
메시 밀어낸 호날두…역대 챔스리그 한 시즌 최다골 1∼3위 독식(종합)
http://sports.news.naver.com/wfootball/news/read.nhn?oid=001&aid=0010020684
‘에어 호날두’ 있기에···레알 8년 연속 챔스 4강
http://sports.news.naver.com/wfootball/news/read.nhn?oid=011&aid=0003268539
[UEL] '황희찬 복귀' 잘츠부르크, 역전 드라마 가능할까?
http://sports.news.naver.com/wfootball/news/read.nhn?oid=436&aid=0000028419
[UCL 포커스] 호날두 환상골도, 만주키치 저력도, 부폰에 묻혔다
http://sports.news.naver.com/wfootball/news/read.nhn?oid=139&aid=0002089597
[UCL 핫피플] ‘120골+11G 연속골’ 호날두, 역사는 진행형
http://sports.news.naver.com/wfootball/news/read.nhn?oid=139&aid=0002089596
[SPO 이슈] ‘재점화’ 케인vs살라, 득점왕 경쟁 포인트3
http://sports.news.naver.com/wfootball/news/read.nhn?oid=477&aid=0000118254
UCL 8강 키워드: 공은 둥글다…'3점 차'여도 안심할 수 없었으니까
http://sports.news.naver.com/wfootball/news/read.nhn?oid=477&aid=0000118253
"과르디올라, 시즌 종료 전 1년 계약 연장한다" (英 미러)
http://sports.news.naver.com/wfootball/news/read.nhn?oid=529&aid=0000022390
케이토토 승무패 14회차 투표율 중간집계
http://sports.news.naver.com/wfootball/news/read.nhn?oid=382&aid=0000638196
‘월드컵 스카우팅 리포트 2018’ 발간
http://sports.news.naver.com/wfootball/news/read.nhn?oid=005&aid=0001088317
레알 마드리드, 천신만고 끝에 챔피언스리그 4강
http://sports.news.naver.com/wfootball/news/read.nhn?oid=052&aid=0001134496
在这里,item
是具有以下格式的字典:
{'aid': '0002185662',
'datetime': '2018.04.12 14:09',
'officeName': '오마이뉴스',
'oid': '047',
'sectionName': '챔스·유로파',
'subContent': '최고의 명경기 예약, 추첨은 4월 13일 오후 7시[오마이뉴스 이윤파 기자]이변이 가득했던 17-18 UEFA 챔피언스리그 8강이 모두 끝나고 4강 진출 팀...',
'thumbnail': 'http://imgnews.naver.net/image/thumb154/047/2018/04/12/2185662.jpg',
'title': '드디어 정해진 UCL 4강 진출팀, 4강 대진은 과연?',
'totalCount': 0,
'type': 'PHOTO',
'url': None}
因此,您可以使用它访问任何您想要的内容。只需替换 ['title']
随心所欲。每个<li>
里面的所有东西标签在字典中可用。
关于python - BeautifulSoup 爬虫解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49787982/
HTML 在 div 中包含字符串: 'div class="slide"' 'img src="xttps://site.com/files/r_1000,kljg894/43k5j/35h43jk
我用这个方法 allcity = dom.body.findAll(attrs={'id' : re.compile("\d{1,2}")}) 返回这样的列表: [掳虏驴碌路驴碌脴虏煤脨脜脧垄脥酶 隆
我正在使用 Jupyter 笔记本、Python 3.5 和虚拟环境。 在我的虚拟环境中,我做了: (venv) > pip install BeautifulSoup4 这似乎运行良好 b/c 终端
我打算用 GUI 制作一个字典程序,但我在第一个障碍上就失败了。我刚刚安装了一个模块( PyDictionary ),但是当我运行以下代码时出现错误。 from PyDictionary import
我正在将一些解析器从 BeautifulSoup3 迁移到 BeautifulSoup4,我认为考虑到 lxml 非常快并且它是我在 BS4 中使用的解析器,分析它会变得多快是个好主意,这里是分析结果
这个问题在这里已经有了答案: Getting Python error "from: can't read /var/mail/Bio" (6 个答案) 关闭 11 个月前。 From Beauti
目前我无法输入这个,因为根据 top,我的处理器是 100%,我的内存是 85.7%,都被 python 占用了。 为什么?因为我让它通过一个 250 兆的文件来删除标记。 250兆,就是这样!我一直
我写了下面的代码: from bs4 import BeautifulSoup import sys # where is the sys module in the source code fold
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我正在为一个项目使用 BeautifulSoup。这是我的 HTML 结构 John Sam Bailey Jack
这段代码正确地从我的博客中提取了马拉地语文本。我很欣赏使用漂亮的汤和正则表达式是多么容易。 from bs4 import BeautifulSoup import requests, re url
我想获取 HTML 中隐藏输入字段的值。 我想用 Python 编写一个正则表达式,它将返回 fooId 的值,前提是我知道 HTML 中的行遵循以下格式 有人可以提供一个 Python 示例来解
我正在尝试获取特定月份的所有链接、标题和日期,例如网站上的三月,我正在使用 BeautifulSoup 这样做: from bs4 import BeautifulSoup import reques
我正试图通过此链接收集有关 2020 年世界上收入最高的运动员收入的信息 https://www.forbes.com/profile/roger-federer/?list=athletes这是第一
我正在尝试从带有美丽汤的网页中捕获所有相关链接。我需要的所有链接都有 class="btn btn-gray"还有文字 More Info<> 仅提取这些链接的最佳方法是什么? 最佳答案 这个怎么样?
我正在尝试抓取一个具有下拉菜单的站点,用户可以在其中选择要显示的数据的年份。但是,我似乎被困在我的实现中。 这是网站网址:https://www.pgatour.com/tournaments/mas
我正在使用 BeautifulSoup 和 mechanise 从网页中查找一些内容。问题是有时找不到我正在寻找的字符串。我不知道有什么问题 对于许多网页,它可以正常工作数月,但突然停止工作。然后我必
( 更新代码 就在下面) 我有一个类:UrlData , 生成一个 url 列表: for url in urls: rawMechSiteInfo = mech.open(url) #me
这是我到目前为止所做的事情: import requests from bs4 import BeautifulSoup URL = "https://www.google.com/search?q=
我是一名优秀的程序员,十分优秀!