51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1568|回复: 6

请问如何能把news_detail4也一起进行比较?

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:28
  • 签到天数: 385 天

    连续签到: 4 天

    [LV.9]测试副司令

    发表于 2020-5-22 13:59:43 | 显示全部楼层 |阅读模式
    1测试积点
    python的返回值news_detail4无法被get_equal_rate_1认定为字符串,请问如何能把news_detail4也一起进行比较?
    我这里是先获取新闻网页内容,然后进行比较,前三个爬取返回值可以进行比较,第四个不行,请问该怎么办?

    1. <p>import difflib
    2. from xml.etree.ElementTree import tostring
    3. import requests
    4. from lxml import etree
    5. import time
    6. from gne import GeneralNewsExtractor
    7. from selenium.webdriver import Chrome
    8. from selenium.webdriver.chrome.options import Options
    9. def get_chinanew_data():
    10. cookies = {
    11. 'Hm_lvt_0da10fbf73cda14a786cd75b91f6beab': '1587367903',
    12. 'Hm_lpvt_0da10fbf73cda14a786cd75b91f6beab': '1587375545',
    13. }
    14. headers = {
    15.     'Connection': 'keep-alive',
    16.     'Cache-Control': 'max-age=0',
    17.     'Upgrade-Insecure-Requests': '1',
    18.     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
    19.     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    20.     'Accept-Language': 'zh-CN,zh;q=0.9',
    21. }</p><p>response = requests.get('http://www.chinanews.com/gn/2020/04-20/9162019.shtml', headers=headers, cookies=cookies,
    22.                         verify=False)
    23. html = response.content.decode(errors='ignore')
    24. etree_html = etree.HTML(html)
    25. main = etree_html.xpath('//div[@id="cont_1_1_2"]')[0]
    26. title = main.xpath('./h1/text()')[0]
    27. pub_time = main.xpath(".//div[3]/div[@class='left-t']/text()")[0]
    28. author = main.xpath('./div[5]/div[2]/div/span/text()')[0][:-2].split(':')[1]
    29. pubtime = pub_time.split()[0] + ' ' + pub_time.split()[1]
    30. content = ''.join(main.xpath('./div[@class="left_zw"]/p/text()')).strip()
    31. site_url = 'http://www.chinanews.com/gn/2020/04-20/9162019.shtml'
    32. site_name = '中国新闻网'
    33. news_detail = {
    34.     'pub_time': pubtime.replace('年', '-').replace('月', '-').replace('日', ''),
    35.     'author': author,
    36.     'title': title,
    37.     'content': content.replace('\u3000', ''),
    38.     'site_url': site_url,
    39.     'site_name': site_name,
    40. }
    41. return news_detail
    42. def selenium_download_data():
    43. options = Options()
    44. options.add_argument('--headless')
    45. driver = Chrome(options=options,executable_path=r"C:\Users\常乐添\AppData\Local\Google\Chrome\Application\chromedriver.exe")
    46. url_list = [
    47. 'https://news.sina.com.cn/gov/xlxw/2020-04-20/doc-iircuyvh8766402.shtml',
    48. 'https://news.ifeng.com/c/7vovtvQ2gVc',
    49. 'https://baijiahao.baidu.com/s?id=1664460259411900230&wfr=spider&for=pc']</p>
    复制代码



    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    8 小时前
  • 签到天数: 1648 天

    连续签到: 5 天

    [LV.Master]测试大本营

    发表于 2020-5-25 09:17:36 | 显示全部楼层
    return difflib.SequenceMatcher(None, str1, str2).quick_ratio()
    ->
    return str(difflib.SequenceMatcher(None, str1, str2).quick_ratio())
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2024-3-11 11:36
  • 签到天数: 999 天

    连续签到: 2 天

    [LV.10]测试总司令

    发表于 2020-5-25 09:49:11 | 显示全部楼层
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2024-3-11 10:55
  • 签到天数: 643 天

    连续签到: 1 天

    [LV.9]测试副司令

    发表于 2020-5-25 09:53:45 | 显示全部楼层
    来学习
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    10 小时前
  • 签到天数: 2637 天

    连续签到: 5 天

    [LV.Master]测试大本营

    发表于 2020-5-25 11:51:39 | 显示全部楼层
    学习
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    8 小时前
  • 签到天数: 863 天

    连续签到: 3 天

    [LV.10]测试总司令

    发表于 2020-5-25 15:51:45 | 显示全部楼层
    学习
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-3-29 18:08 , Processed in 0.065522 second(s), 21 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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