鱼C论坛

 找回密码
 立即注册
查看: 1409|回复: 2

[已解决]关于python 0基础第55课课后题

[复制链接]
发表于 2020-3-23 20:57:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
第55课的课后题动动手第一题要求用户可以自己输入内容,然后爬虫百度百科
根据小甲鱼的答案运行不了,应该是网址变了,所以请问百度百科的新网址应该怎么看?
最佳答案
2020-3-23 21:09:13
本帖最后由 Hoiste 于 2020-3-23 21:11 编辑

是改了,我记得答案好像只能搜出多肉百科和恐龙百科了,你如果看过网页源代码就会发现那些词条已经换了关键字了(view变成item),这是我当初学的时候根据情况改的代码,你看看能不能用,另外,后面那题同样也不能按答案来了,我也放上来吧。
1题:
  1. import urllib.request
  2. import urllib.parse
  3. import re
  4. from bs4 import BeautifulSoup

  5. def main():
  6.     keyword = input('请输入关键词:')
  7.     keyword = urllib.parse.urlencode({'word':keyword})
  8.     url = 'http://baike.baidu.com/search/word?%s' %keyword
  9.     response = urllib.request.urlopen(url)
  10.     html = response.read()
  11.     soup = BeautifulSoup(html,'html.parser')
  12.     #百度百科下该关键词作为多义词时的各个词条
  13.     for each in soup.find_all(href = re.compile('item')):
  14.         content = each.text
  15.         try:
  16.             url2 = ''.join(['http://baike.baidu.com',each['href']])
  17.             response2 = urllib.request.urlopen(url2)
  18.         except UnicodeEncodeError:
  19.             temp = each['href'].split('/')[2]
  20.             key = urllib.parse.urlencode({'':temp}).split('=')[1]
  21.             each['href'] = ''.join(['/item/',key])
  22.             url2 = ''.join(['http://baike.baidu.com',each['href']])
  23.             response2 = urllib.request.urlopen(url2)
  24.         finally:
  25.             html2 = response2.read().decode('utf-8')
  26.             soup2 = BeautifulSoup(html2,'html.parser')
  27.             
  28.         if soup2.h2:
  29.             content = ''.join([content,soup2.h2.text])
  30.         content = ''.join([content,' -> ',url2])
  31.         print(content)



  32. if __name__ == '__main__':
  33.     main()

复制代码


2题
  1. import urllib.request
  2. import urllib.parse
  3. import re
  4. from bs4 import BeautifulSoup

  5. def get_url(soup):
  6.     for each in soup.find_all(href=re.compile('item')):
  7.         content = ''.join([each.text])
  8.         try:
  9.             url2 = ''.join(['http://baike.baidu.com',each['href']])
  10.             response2 = urllib.request.urlopen(url2)
  11.         except UnicodeEncodeError:
  12.             temp = each['href'].split('/')[2]
  13.             each['href'] = urllib.parse.urlencode({'':temp}).split('=')[1]
  14.             url2 = ''.join(['http://baike.baidu.com','/',each['href']])
  15.             response2 = urllib.request.urlopen(url2)
  16.         finally:
  17.             html2 = response2.read()
  18.             soup2 = BeautifulSoup(html2,'html.parser')

  19.         if soup2.h2:
  20.             content = ''.join([content,soup2.h2.text])
  21.         content = ''.join([content,' -> ',url2])
  22.         yield content


  23. def main():
  24.     keyword = input('请输入关键词:')
  25.     keyword = urllib.parse.urlencode({'word':keyword})
  26.     url = 'http://baike.baidu.com/search/word?%s' %keyword
  27.     response = urllib.request.urlopen(url)
  28.     html = response.read()
  29.     soup = BeautifulSoup(html,'html.parser')

  30.     result = soup.find(text=re.compile('百度百科尚未收录词条'))
  31.     if result:#如果未收录打印没有收录词条
  32.         result = result.split(' ”')[0]#去掉奇怪的小尾巴
  33.         print(result)
  34.     else:#没有未收录词条的内容则打印标题、简介
  35.         title = soup.h1.text
  36.         if soup.h2:
  37.             title += soup.h2.text
  38.         print(title)
  39.         if soup.find(class_='lemma-summary'):
  40.             print(soup.find(class_='lemma-summary').text)

  41.         print('下边打印相关链接:')

  42.         #打印十个链接。。。好像要另起函数比较方便使用生成器来解决问题
  43.         each = get_url(soup)
  44.         while True:
  45.             try:
  46.                 for i in range(10):
  47.                     print(next(each))
  48.             except StopIteration:
  49.                 break

  50.             Quit = input('任意键继续打印词条,按q取消:')
  51.             if Quit == 'q':
  52.                 break
  53.             else:
  54.                 continue


  55. if __name__ == '__main__':
  56.     main()

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-23 21:09:13 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Hoiste 于 2020-3-23 21:11 编辑

是改了,我记得答案好像只能搜出多肉百科和恐龙百科了,你如果看过网页源代码就会发现那些词条已经换了关键字了(view变成item),这是我当初学的时候根据情况改的代码,你看看能不能用,另外,后面那题同样也不能按答案来了,我也放上来吧。
1题:
  1. import urllib.request
  2. import urllib.parse
  3. import re
  4. from bs4 import BeautifulSoup

  5. def main():
  6.     keyword = input('请输入关键词:')
  7.     keyword = urllib.parse.urlencode({'word':keyword})
  8.     url = 'http://baike.baidu.com/search/word?%s' %keyword
  9.     response = urllib.request.urlopen(url)
  10.     html = response.read()
  11.     soup = BeautifulSoup(html,'html.parser')
  12.     #百度百科下该关键词作为多义词时的各个词条
  13.     for each in soup.find_all(href = re.compile('item')):
  14.         content = each.text
  15.         try:
  16.             url2 = ''.join(['http://baike.baidu.com',each['href']])
  17.             response2 = urllib.request.urlopen(url2)
  18.         except UnicodeEncodeError:
  19.             temp = each['href'].split('/')[2]
  20.             key = urllib.parse.urlencode({'':temp}).split('=')[1]
  21.             each['href'] = ''.join(['/item/',key])
  22.             url2 = ''.join(['http://baike.baidu.com',each['href']])
  23.             response2 = urllib.request.urlopen(url2)
  24.         finally:
  25.             html2 = response2.read().decode('utf-8')
  26.             soup2 = BeautifulSoup(html2,'html.parser')
  27.             
  28.         if soup2.h2:
  29.             content = ''.join([content,soup2.h2.text])
  30.         content = ''.join([content,' -> ',url2])
  31.         print(content)



  32. if __name__ == '__main__':
  33.     main()

复制代码


2题
  1. import urllib.request
  2. import urllib.parse
  3. import re
  4. from bs4 import BeautifulSoup

  5. def get_url(soup):
  6.     for each in soup.find_all(href=re.compile('item')):
  7.         content = ''.join([each.text])
  8.         try:
  9.             url2 = ''.join(['http://baike.baidu.com',each['href']])
  10.             response2 = urllib.request.urlopen(url2)
  11.         except UnicodeEncodeError:
  12.             temp = each['href'].split('/')[2]
  13.             each['href'] = urllib.parse.urlencode({'':temp}).split('=')[1]
  14.             url2 = ''.join(['http://baike.baidu.com','/',each['href']])
  15.             response2 = urllib.request.urlopen(url2)
  16.         finally:
  17.             html2 = response2.read()
  18.             soup2 = BeautifulSoup(html2,'html.parser')

  19.         if soup2.h2:
  20.             content = ''.join([content,soup2.h2.text])
  21.         content = ''.join([content,' -> ',url2])
  22.         yield content


  23. def main():
  24.     keyword = input('请输入关键词:')
  25.     keyword = urllib.parse.urlencode({'word':keyword})
  26.     url = 'http://baike.baidu.com/search/word?%s' %keyword
  27.     response = urllib.request.urlopen(url)
  28.     html = response.read()
  29.     soup = BeautifulSoup(html,'html.parser')

  30.     result = soup.find(text=re.compile('百度百科尚未收录词条'))
  31.     if result:#如果未收录打印没有收录词条
  32.         result = result.split(' ”')[0]#去掉奇怪的小尾巴
  33.         print(result)
  34.     else:#没有未收录词条的内容则打印标题、简介
  35.         title = soup.h1.text
  36.         if soup.h2:
  37.             title += soup.h2.text
  38.         print(title)
  39.         if soup.find(class_='lemma-summary'):
  40.             print(soup.find(class_='lemma-summary').text)

  41.         print('下边打印相关链接:')

  42.         #打印十个链接。。。好像要另起函数比较方便使用生成器来解决问题
  43.         each = get_url(soup)
  44.         while True:
  45.             try:
  46.                 for i in range(10):
  47.                     print(next(each))
  48.             except StopIteration:
  49.                 break

  50.             Quit = input('任意键继续打印词条,按q取消:')
  51.             if Quit == 'q':
  52.                 break
  53.             else:
  54.                 continue


  55. if __name__ == '__main__':
  56.     main()

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-23 21:12:33 | 显示全部楼层
Hoiste 发表于 2020-3-23 21:09
是改了,我记得答案好像只能搜出多肉百科和恐龙百科了,你如果看过网页源代码就会发现那些词条已经换了关键 ...

可以了,感谢!
第0题的时候我也会莫名其妙出来恐龙和多肉这种不相关的东西。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 10:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表