Python 美化群爬虫解析

Python 美化群爬虫解析,python,beautifulsoup,Python,Beautifulsoup,我正在尝试使用bs4制作一个爬虫程序 这是我要从中抓取数据的目标页面 这就是我想要得到的数据 <html>~~ <head>...</head> <body> <some several layer...> <div class="news_list" id="_newsList"> <h3 class="blind"> ~ </h3>

我正在尝试使用bs4制作一个爬虫程序 这是我要从中抓取数据的目标页面

这就是我想要得到的数据

<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>
和结果

[]
我该怎么办?

这样就行了

import requests
from bs4 import BeautifulSoup
targettrl = 'http://sports.news.naver.com/wfootball/news/index.nhn?page=1'
soup = BeautifulSoup(requests.get(targettrl).content, 'html.parser')
print(soup.find_all("div", class_="news_list"))
试一试

而不是

soup = BeautifulSoup(urllib.urlopen(targettrl).read(), 'html.parser')

我尝试了你的代码和@KeyurPotdar,并为我工作。

你要查找的内容是用JavaScript动态加载的,因此在你搜索的
标记内的页面源代码中找不到。但是,数据以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']
替换为您想要的内容即可。每个
  • 标签中的所有内容都可以在字典中找到。

    你是说你想要
    标签中的所有内容,包括
    html标签
    ?你只想要标题吗?@KeyurPotdar nah我想要
      标签中的所有内容,我是指
    • 标签列表(包括其内容)@萨尔曼,是的,我想我解决了问题。我认为,特定的
      div
      和相应的
      div
      的数据都是从
      JavaScript
      生成的,因为我只能够抓取其上一级的数据,即
      d=item.find_all(“div”,“class”:“content\u area”})
      ,但是这个div的子级是空的!相同的结果:-(@KeyurPotdar)你能显示你想要抓取的网站数据(不是源代码)的截图吗?编辑你的问题并添加截图there@ParkGunTae你能显示网站数据的截图吗(不是源代码)你想抓取什么?编辑你的问题,然后在那里添加屏幕截图,这是行不通的!问题也会出现,因为它正在解析页面,但无法选择特定的
      div标记。
      。做得好!我就快到了,只是想不起JSON部分。太好了!!这很有帮助,但你是怎么发现的?我是说,y是怎么发现的你认为它在脚本中的页面源代码中是可用的。我不明白。我刚刚开始学习编程,它太难了。只需打开页面源代码并使用ctrl-F搜索字符串
      ‘로마 영웅’ 제코, “첼시 거절? 돈이 중요하지 않아”
      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
      
      {'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}